I'm trying to save image in a folder using relative path. My code is
bmp1.Save(#"~/mintemp/" + filename, jgpEncoder, myEncoderParameters);
I'm getting error like this.
A generic error occurred in GDI+
But when i change path like below
bmp1.Save(#"E:\web data\website\mintemp\" + filename, jgpEncoder, myEncoderParameters);
Code working fine in absolute path. Can anyone help me to solve out this issue becaue i want to save using relative path. Thanks in advance, sorry for my bad English.
Try using Server.MapPath. The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
Note that, Server.MapPath(#"~/mintemp/") returns the physical path of the folder mintemp present in the root directory of the application
bmp1.Save(Server.MapPath(#"~/mintemp/") + filename, jgpEncoder, myEncoderParameters);
You can use Server.MapPath function to get the relative path.
In your case it'll be : Server.MapPath("E:\web data\website\mintemp\" + filename). It return a string that you can use in your Save method.
More explanations here : https://forums.asp.net/t/1813648.aspx?Any+difference+between+Server+MapPath+and+Server+MapPath+
Related
This is my first time trying to use images in my code. I cannot figure out what file the FromFile command pulls from.
firstDice.Image = Image.FromFile(fDice.ToString() + ".png");
I am trying to get the image to correspond with whatever the random number fDice is.
Here is my error message:
System.IO.FileNotFoundException: '5.png'
Environment.CurrentDirectory
As Steve says in his comment, you must specify the full path otherwise.
You can use Path.Combine to create a path based on the current working directory. Such as:
Path.Combine(Environment.CurrentDirectory, "Images");
I have an MVC project and a class library just for saving and deleting images.
I have the path to those images stored in a variable as a relative path
Content\images\ that I reference inside the Save() and Delete() methods.
The save method works as I would think but the delete throws an error as it's relating the current path from the window directory.
// Works fine
File.WriteAllBytes(Path.Combine(Settings.ImagesPath, filename), binaryData);
// Error saying it cannot find the path in the C:\windows\system32\folder
File.Delete(Path.Combine(Settings.ImagesPath, filename));
I'd like to be able to switch between relative and absolute paths in my Settings.ImagesPath string but every SO article I've tried works for one scenario or the other. What's the best way to convert absolute or relative paths to some common way to deal with them?
You should use Server.MapPath method to generate the path to the location and use that in your Path.Combine method.
var fullPath = Path.Combine(Server.MapPath(Settings.ImagesPath), filename);
System.IO.File.Delete(fullPath);
Server.MapPath method returns the physical file path that corresponds to the specified virtual path. In this case, Server.MapPath(Settings.ImagesPath) will return the physical file path to your Content\images\ which is inside your app root.
You should do the same when you save the file as well.
You can also check the existence of the file before attempting to delete it
var fullPath = Path.Combine(Server.MapPath(Settings.ImagesPath), filename);
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
}
Server.MapPath expects a relative path. So if you have an absolute value in the Settings.ImagePath, You can use the Path.IsPathRooted method to determine if it is a virtual path or not
var p = Path.Combine(Path.IsPathRooted(Settings.ImagesPath)
? path : Server.MapPath(Settings.ImagesPath), name);
if (System.IO.File.Exists(p))
{
System.IO.File.Delete(p);
}
When you use the virutal path, make sure it start with ~.
Settings.ImagesPath = #"~\Contents\Pictures";
I'd like to save an uploaded file to a physical path by the method HttpPostedFileBase.SaveAs().
When I choose a physical path, an exception appears indicates that the path must be virtual.
var fileName = Path.GetFileName(fileurl.FileName);
var path = "C:/Projets" + fileName;
fileurl.SaveAs(Server.MapPath(path));
How can I change my code to be able to save the file every where I want?
The Server.MapPath works only with physical locations that are part of the website. If you want to save the file outside you could use the following:
var fileName = Path.GetFileName(fileurl.FileName);
fileurl.SaveAs(Path.Combine(#"c:\projects", fileName));
Make sure though that the account under which your application pool is executing is granted write permissions to this folder.
Server.MapPath is for virtual path. You can try to use Path.GetFullPath(path).
my code is:
string filename = FileUploader.PostedFile.FileName.Substring(fuImage.PostedFile.FileName.LastIndexOf("\\") + 1);
if(fuImage.HasFile)
{
FileUploader.SaveAs(Server.MapPath("Modules/NewUserProfile/UserPic/" + filename));
imgUser.ImageUrl = Server.MapPath("Modules/NewUserProfile/UserPic/" + filename);
}
imgUser is id of asp:Image Control.Image is uploaded in desire folder but its not display image in image control.What i am doing wrong here? Is there any postback issue.Thanks.
Use relative path for ImageUrl property.
imgUser.ImageUrl = "Modules/NewUserProfile/UserPic/" + filename;
OR use root operator ~ if Modules folder is located at root of web-app.
imgUser.ImageUrl = "~/Modules/NewUserProfile/UserPic/" + filename;
To run this code please understand the following things:
Server.MapPath() is used for getting physical Path like: D:/Img/Upload/..
so it is good idea to get path for saving image.
But in the case when you are getting the image for binding it to image control then you must have to use virtual path instead of Physical path.
Virtual path like: localhost/demo/upload/myimage.jpg.
I usually debug problems like this by first doing a view-source with your browser, and making sure the URL in the HTML source is what you expect. Then try copying the url that shows up in your view-source and pasting it into the address bar of your browser, and see if it shows up.
I think you should wait for the image to completely upload and then set the Image path to the image.
Also after rendering into the Browser use FireBug in Mozilla or Chrome Inspect Elemnt to check whether the Image has been rendered properly by looking at the image. If it broken then your image path is wrong. Try to give relative path and check.
newpic.ImageUrl = Page.ResolveURL("~/Pictures")+filename;
Server.MapPath returns physical drive location which is not useful when you are assigning to the imageurl.
I have to fetch all the files from a folder and i am using the function GetFiles() like
string[] dirImages = Directory.GetFiles(strPathYearImages + intYear , "*.png");
where strPathYearImages="Images\Holiday\2010\"
but when i write the whole path like
string[] dirImages = Directory.GetFiles(#"E:\IWP\Images\Holiday\"+ intYear , "*.png");
i get the required result.
How to solve this problem? I dont want to use the whole path.
Help me out.
Regards,
Jigar <3
The documentation for GetFiles() says:
The path parameter is permitted to
specify relative or absolute path
information. Relative path information
is interpreted as relative to the
current working directory
So you would want to make sure the current working directory is set correctly before trying to use the relative paths (eg to E:\IWP):
GetCurrentDirectory
SetCurrentDirectory
Use Application.StartupPath to get the location where the executable is running. From there you need to know where the images directory is relative to that directory. The only other option is the absolute path. How else would it know where to look?
You can also try using System.IO.Path methods to help - especially for combining path strings, plus it also gives you the location of special folders like My Documents, AppData, and the desktop.
Maybe it's because you are running it from VS inside. Your executable is in ProjectName\bin\Debug, therefore it looks for ProjectName\bin\Debug\Images, which obviously does not exists.
The problem is the first snippet tries to get the images under current path. So you could tell the images path relative to your current path.
Hey all, i got the answer to my question.
I just had to write
string[] dirImages = HttpContext.Current.Server.MapPath(strPathImages + intYear , "*.png");
Hope that it is helpful to all...