Question

I have this code that I use to resize and save a file that is posted by the user. The issue is that when I rezise to a 480px width the image looses lots of quality and the size in kb is still pretty big.

For instance, when I resize the same image to 480px "by hand" using a software like Paint, the quality is still as good as the original (from what my eye can tell) and the size in kb is a lot smaller than resizing using the GetThumbNailImage method.

Mdn says "If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.", but that seems to be for Windows forms and I need for a web app.

What code should I use to do this then?

System.IO.Stream st = FileUploadPost.PostedFile.InputStream;

myImage = System.Drawing.Image.FromStream(st);
thumb = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);

thumb.Save(myPath);
Was it helpful?

Solution

Here is code that has worked for me. You can set the new bitmap resolution:

using System.Drawing;

Bitmap img = (Bitmap)Bitmap.FromStream(FileUploadPost.PostedFile.InputStream);
Bitmap newImg = new Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newImg.SetResolution(72, 72);
Graphics newGraphic = Graphics.FromImage(newImg);
newGraphic.Clear(Color.Transparent);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(img, 0, 0, maxWidth, maxHeight);
System.Drawing.Imaging.ImageFormat format = default(System.Drawing.Imaging.ImageFormat);
string ext = Path.GetExtension(FileUploadPost.PostedFile.FileName);
switch (ext.ToLower())
{
    case ".gif":
        format = System.Drawing.Imaging.ImageFormat.Gif;
        break;
    case ".png":
        format = System.Drawing.Imaging.ImageFormat.Png;
        break;
    default:
        format = System.Drawing.Imaging.ImageFormat.Jpeg;
        break;
}
newImg.Save(myPath, format);

You can wrap it in a void function on a global class:

public static void UploadImage(HttpPostedFileBase file, int maxWidth, int maxHeight)
{
  //paste all the above code in here and replace FileUploadPost.PostedFile with file
}

Then you can call it from anywhere in your project:

ClassName.UploadImage(FileUploadPost.PostedFile, 300, 300);

OTHER TIPS

Does this answer sufficiently?

Resizing an image in asp.net without losing the image quality

This is an issue that comes up fairly frequently.

Try this

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public static System.Drawing.Image ResizeImage( System.Drawing.Image image, int percent ) {
  // percent is the actual integer percent of the original size

  System.Drawing.Bitmap imgThumb = new System.Drawing.Bitmap( image.Width * percent / 100, image.Height * percent / 100 );

  Rectangle sourceRect = new Rectangle( 0, 0, image.Width, image.Height );
  Rectangle destRect = new Rectangle( 0, 0, imgThumb.Width, imgThumb.Height );


  System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( imgThumb );
  g.CompositingQuality = CompositingQuality.HighQuality;
  g.SmoothingMode = SmoothingMode.HighQuality;
  g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  g.DrawImage( image, destRect, sourceRect, GraphicsUnit.Pixel );
  return ( imgThumb );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top