Resize image without loss of it original quality. Here i'm resizing a big image to (410,90).
private void MakeLarge()
{
System.Drawing.Image myThumbnail150;
object obj = new object();
obj = fuPhoto;
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
HtmlInputFile hFile = (HtmlInputFile)obj;
if (hFile.PostedFile != null && hFile.PostedFile.ContentLength > 0)
{
//this code used to remove some symbols between image name and replace with space
string imgname1 = hFile.PostedFile.FileName.Replace('%', ' ').Substring(hFile.PostedFile.FileName.LastIndexOf("\\") + 1);
string imgname2 = imgname1.Replace('#', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname3 = imgname2.Replace('@', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname4 = imgname3.Replace(',', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname5 = imgname4.Replace('&', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
Finalimagename = imgname5.ToString();
string imgname = hFile.PostedFile.FileName.Substring(hFile.PostedFile.FileName.LastIndexOf("\\") + 1);
string sExtension = imgname.Substring(imgname.LastIndexOf(".") + 1);
//this code is used to check image extension
if (sExtension.ToLower() == "jpg" || sExtension.ToLower() == "gif" || sExtension.ToLower() == "bmp" || sExtension.ToLower() == "jpeg")
{
hFile.PostedFile.SaveAs(ResolveUrl(Server.MapPath("~/Product_Photo_Large/" + Finalimagename)));
//imgOriginal = "Product_Photo_Large\\" + Finalimagename;
System.Drawing.Image imagesize = System.Drawing.Image.FromFile(Server.MapPath("~/Product_Large\\" + fuPhoto.PostedFile.FileName));
Bitmap bitmapNew = new Bitmap(imagesize);
if (imagesize.Width < imagesize.Height)
{
myThumbnail150 = bitmapNew.GetThumbnailImage(390 * imagesize.Width / imagesize.Height, 390, myCallback, IntPtr.Zero);
// myLarge = bitmapNew.GetThumbnailImage(410, 310, myCallback, IntPtr.Zero);
}
else if (imagesize.Width > imagesize.Height)
{
myThumbnail150 = bitmapNew.GetThumbnailImage(410, 410 * imagesize.Height / imagesize.Width, myCallback, IntPtr.Zero);
// myLarge = bitmapNew.GetThumbnailImage(410, 310, myCallback, IntPtr.Zero);
}
else
{
myThumbnail150 = bitmapNew.GetThumbnailImage(410, 390, myCallback, IntPtr.Zero);
}
//Create a new directory name ThumbnailImage
//Directory.CreateDirectory(Server.MapPath("ThumbnailImage"));
//Save image in TumbnailImage folder
myThumbnail150.Save(ResolveUrl(Server.MapPath("~/Resize\\")) + Finalimagename, System.Drawing.Imaging.ImageFormat.Jpeg);
imgOriginal = "Resize\\" + Finalimagename;
// MessageLabel.Text = "Successfully uploaded";
}
else
{
// lblError1.Visible = true;
//lblError1.Text = "Check image extension";
}
}
}
No comments:
Post a Comment