quality problem when resizing an image file


Sinan Alkan

Hi All,

I have a method to resize any image file to the specific dimensions and
save this new image to a path.
I found it on a web page(By Joel Neubeck) and i changed it for my project.
The code is as follows;
// usings part
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

// The method
/// <summary>
/// </summary>
/// <param name="imgPhoto">Original Image</param>
/// <param name="Width">Width of the new image</param>
/// <param name="Height">Height of the new image</param>
/// <param name="Path">The path that new image will be saved</param>
/// <param name="name">The File Name of the original file without
/// <param name="Subfix">a string to add the end of the file name when
saving as new image file</param>
/// <param name="EXT">Extension of the original file</param>
/// <returns></returns>

public static bool CreateFixedSize(Image imgPhoto, int Width, int Height,
string Path,
string name, string Subfix, string EXT)
ImageFormat kk = imgPhoto.RawFormat;
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode =
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
bmPhoto.Save(Path + @"\" + name + Subfix + EXT, kk);
return true;
return false;

sample usage is ;
clsImage.CreateFixedSize(imgOrg, 480, 480, txtPath, txtFileName, "_L",

It works and creates new one but the problem is that the quality of new
image become bad then original file.
How can i get same quality as original file.

Anybody can give me a hand for this ?

Thanks in advance.




Roger Frost

It works and creates new one but the problem is that the quality of new
image become bad then original file.
How can i get same quality as original file.

Anybody can give me a hand for this ?

When you loose quality, are you enlarging or reducing the image size?
Preserving aspect ratio?

I don't know anything about graphics processing, I'm lucky to take a good
picture on a sunny day, but just looking over your code, I would probably
start with the formatting differences. Changing an image's format multiple
times might reduce quality even if you don't touch it with anything else
(actual pixel editing). But also good codex's should reduce the effect of
this. Another factor might the quality of the image you have to begin with,
perhaps something can look good even if it's not good quality (ie.
compression)?...until you start editing it.. I would like to know more
about this myself. Yeah, I know, format determines compression, but I'm
trying to express an idea for which I have no words. ....Dangerous

The real reason why I'm replying...it looks as if you're just storing a
Bitmap with a jpg file extension (per your example, of course you could pass
".xyz" as a file extension as well) ...this doesn't make it an actual JPEG,
but I have no doubt that several (if not all) image viewers will still
display it correctly, as they (should) rely on the file header and not the
name or extension to interpret how to decode the file... You should
implement different image formats (and limit the choices to those
implemented), or remove the parameter for file extension and save everything
as bmp...in a round about way this should be removed anyhow, format and
environment should govern the extension assigned, not the other way around,
this will make your (or Joel's) code more portable.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question