By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

quality problem when resizing an image file

P: n/a
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
extension</param>
/// <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);
}
else
{
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,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolutio n,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
try
{
bmPhoto.Save(Path + @"\" + name + Subfix + EXT, kk);
bmPhoto.Dispose();
return true;
}
catch
{
bmPhoto.Dispose();
return false;
}
}
---------------------------------

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

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.

ALKAN
Sep 3 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
>...
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.

Sep 3 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.