Reputation: 2760
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?
Upvotes: 0
Views: 1843
Reputation: 5727
Thumbnail will reduce your actual image, so that it will display on avatar of your site without any distortion. You can also use image without creating thumbnail but users will upload images in different rations (width and height) , they might get distorted while displaying. Hence thumbnail will make proportional sizes of all uploaded images.
Upvotes: 1
Reputation: 1038710
Server.MapPath
provides 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:
ImageControl1.ImageUrl = ResolveUrl("~/ThumbNail_" + ImgName);
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:
ImageControl1.ImageUrl = "~/ThumbNail_" + ImgName;
Upvotes: 0