I am using this code to get an image from the contents / IMG folder in Visual Studio:
image image = image.frame file (@ "~ \ content \ img \ toendra.JPG");
It gives me an error that the file is not found. However, if I give the full path to the image, then it works:
image image = image.framefile (@ "c: \ user \ steven \ source \ repo \ groep11DotNet \ P2groep11.Net \ content \ img \ toendra.JPG ");
What is wrong with my relative path?
System.Drawing.Image.FromFile
do not know how to do an asp.net The application has to handle root relative paths. You have to use an intermediate function that can be understood to convert it to a physical file path.
image image = image. Fromphyx (HTTPTtext. Current.sop .Mappath ("~ / content /img/toendra.JPG"));
Note that I have changed your backslash to the forward slash (which are the correct symbols for use in the URL) and eliminated the need for string.
If you are going to use this a lot, you can create assistant helper classes. Public static class ImageHelper {Public static image LoadFromAspNetUrl (string URL) {if (HttpContext.Current == null) {New ApplicationException Throw ("Can not use HttpContext. Non-ASP.NET Current in context "); } Return Image.framefile (htmlctx.content.sysmappath (url)); }}
Usage:
image myImage = ImageHelper.LoadFromAspNetUrl ("~ / content / img / toendra.JPG");
No comments:
Post a Comment