2

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);
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
user2974961
  • 335
  • 1
  • 6
  • 14
  • I have written this function that resizes JPEG images where you can set the quality http://codingstill.com/2011/07/creating-thumbnails-from-jpg-images-with-net/. Hope it helps – Tasos K. Jan 26 '14 at 18:45
  • thanks for your reply codingstill but seems your function is just for jpg images and I work with the other formats as well. – user2974961 Jan 26 '14 at 21:38

3 Answers3

2

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);
Johnny
  • 1,141
  • 9
  • 6
  • 1
    tried this too and it worked well also. My comment is the same for that answered to Ramzi. The image maintains visual quality which is great, but size in kb is very big. While on paint I could reduce the 2MB file to 89kb (when reducing it to 480px), this code reduced to 394kb. – user2974961 Jan 26 '14 at 20:21
0

Does this answer sufficiently?

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

This is an issue that comes up fairly frequently.

Community
  • 1
  • 1
Justin Crabtree
  • 327
  • 1
  • 6
0

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 );
}
Ramzi
  • 1
  • 1
    ok, this worked well. Visual quality remained very good. The issue with the sizing remains though. The original uploaded file is 2.518 kb. After uploading it and using this code to resize it, the new size becomes 421kb. But when I do it manually using paint I can reduce the 2.518kb to 89kb and remain with visual quality. Any ideas? – user2974961 Jan 26 '14 at 19:49