Resize Images Without Loss Of Quality in ASP.NET
To create a image Gallery for your site, you may need to re size your image before uploading. And here is an example for you to do it in code behind. Without losing the original quality of the image resizing can be done.
Here am saving the image path in the database and the image in my folder. Before saving the image, I call the MakeLarge() function which performs my resizing. And here is the function.
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 <>
{
myThumbnail150 = bitmapNew.GetThumbnailImage(410 * imagesize.Width / imagesize.Height, 410, myCallback, IntPtr.Zero);
// myLarge = bitmapNew.GetThumbnailImage(410, 310, myCallback, IntPtr.Zero);
}
else if (imagesize.Width > imagesize.Height)
{
myThumbnail150 = bitmapNew.GetThumbnailImage(390, 390 * imagesize.Height / imagesize.Width, myCallback, IntPtr.Zero);
// myLarge = bitmapNew.GetThumbnailImage(410, 310, myCallback, IntPtr.Zero);
}
else
{
myThumbnail150 = bitmapNew.GetThumbnailImage(390, 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";
}
}
}
And another function to resize your image is as here..
private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
{
Bitmap originalImage = new Bitmap(streamImage);
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
originalImage.Dispose();
return newImage;
}