Friday, June 17, 2011

File Upload: Thumbnail

if(FU_Product.FileName != "")
{
Int32 intFileSize = Convert.ToInt32(FU_Product.PostedFile.ContentLength);
if(intFileSize <= 1000000)
{
String[] fileNameArray = FU_Product.FileName.Split('.');

string fileSavePath;
string origFileName;
string newFileName;

fileSavePath = "Images/";
origFileName = fileNameArray[0].ToString();
newFileName = Guid.NewGuid().ToString() + "." + fileNameArray[1];

FU_Product.SaveAs(Server.MapPath(fileSavePath + newFileName));

System.Drawing.Image Img = System.Drawing.Image.FromStream(FU_Product.PostedFile.InputStream);

Int32 thumbSize = 150;
Int32 width, height;

height = thumbSize;
width = Img.Width * thumbSize / Img.Height;

System.Drawing.Image thumbnailImage = Img.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

thumbnailImage.Save(Server.MapPath(fileSavePath + "T" + newFileName));

parm[4] = new SqlParameter("@Image", SqlDbType.VarChar, 50);
parm[4].Value = newFileName.ToString();

}
else
{
lblError.Text = "File size should be < 1 MB.";
}

}


private bool ThumbnailCallback()
{
return true;
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More