2

In my asp.net application I want to re-size some Images to small dimension, say 75*75 and I want to do it without loosing the image quality. Currently I am using Image re-sizing using Bitmap and the code snippet is given below.

System.Drawing.Image oldImg= System.Drawing.Image.FromFile(Filepath);
Bitmap newImg = new Bitmap(75, 75, PixelFormat.Format24bppRgb);
Graphics g= Graphics.FromImage(newImg);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.DrawImage(oldImg, new Rectangle(new Point(0, 0), newSize));
newImg.Save(Filepath, ImageFormat.Jpeg);

But doing like this, the new image appears blurry. can I get the good image without any blurs using this methods by making any modification or is there any other method to re-size the original image to specified dimension without loosing the quality.

NCA
  • 810
  • 1
  • 8
  • 19
  • What size is the original image? – Chris Ballard Mar 18 '14 at 11:25
  • Are you enlarging an image? In that case the problem is, that most of the pixels in the larger image do not exist in the smaler one. You would have to guess, how the pixels look... – Chrisi Mar 18 '14 at 11:25
  • @Chrisi I think OP is making the image smaller - "I want to re-size some images to small dimension" – Chris Ballard Mar 18 '14 at 11:27
  • Maybe this question will help you out, http://stackoverflow.com/questions/4737220/c-sharp-mvc-image-upload-resizing-server-side – Rob Mar 18 '14 at 11:28
  • @ChrisBallard My oroginal Image is of different larger size and different larger dimension. – NCA Mar 18 '14 at 11:32
  • @Chrisi Nop. I want to make the image smaller size and smaller dimension. My oroginal Image is of different larger size and different larger dimension. – NCA Mar 18 '14 at 11:32

4 Answers4

0

If interpolation is used, the resulting image must necessarily become blurry especially if the target size is significantly larger than the original size (which surprisingly is not the case in your situation). I'm afraid the problem cannot be solved just by choosing a different method. You still do have the option of chosing InterpolationMode.NearestNeighbor which (in a certain sense) is no interpolation; the resulting image then might be blocky instead of blurry (which usually is also not the desired result).

Codor
  • 17,447
  • 9
  • 29
  • 56
0

Saving the image using Jpeg will make the image lose quality every time you save it.
You can find many questions regarding the quality issue on stackiverflow:
https://stackoverflow.com/a/87786/20126
https://stackoverflow.com/a/2320124/20126
https://stackoverflow.com/a/18086739/20126

Community
  • 1
  • 1
Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
0

There are a number of tools you can use to do this. I'm currently using ImageResizer and it's pretty awesome. It takes care of all the nighty gritty details and the docs are great:

ImageResizer

There is also a Nuget package so it can be delivered directly to your project :)

Chris
  • 5,040
  • 3
  • 20
  • 24
0

A simple trick, if you are able to do so, is just to ensure the original image size is an exact multiple of the resized image, eg 150x150 or 300x300 - this ensures that fuzziness due to aliasing and interpolation is minimised.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40