I got this code that should help me save an image to the servers file system:
if (FileUpload1.HasFile)
{
string ImgName = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("~").ToString() + "\\" + ImgName);
Stream imgStream = FileUpload1.PostedFile.InputStream;
Bitmap bmThumb = new Bitmap(imgStream);
System.Drawing.Image im = bmThumb.GetThumbnailImage(100, 100, null, IntPtr.Zero);
im.Save(Server.MapPath("~").ToString() + "\\ThumbNail_" + ImgName);
}
I dont know why they convert the image to a Thumbnail.. All I need is the image to be saved to the files system and retreived as a small image (for avatar)..
I save the filename as nvarchar(50) in my database.
My question is how do I retrieve the image from the filesystem. (I can get its path from the database, which is: Server.MapPath("~").ToString() + "\\ThumbNail_" + ImgName
Is all I need to do:
ImageControl1.imageUrl=Server.MapPath("~").ToString() + "\\ThumbNail_" + ImgName;
Will the image appear in my ImageControl or not.
Can you criticize my code and add to it..cause it is the first time that i try to save and retrieve from the filesystem.
Will all that process work on my visual studio 2010 local server?
Server.MapPathprovides an absolute path to the image on the server. In order to serve the image to the client you need the relative path. So you may try using the ResolveUrl method:or if I remember correctly the Image control already does this and you don’t need to explicitly call
ResolveUrl, so the following might also work: