J
JJ
Whilst I am resizing images I am losing quality. This is only happening in
small amounts, but if you repeatedly put the same image through the
following code, the image quality slowly degrades. Can anyone spot why this
may be happening? Should it only deteriate once upon intitially going
through this procedure? The deterioration is subtle, but after a few
iterations is quite noticeable..:
private static System.Drawing.Image CopyImage(System.Drawing.Image
imageFile, int width, int height)
{
System.Drawing.Bitmap bmpOut;
ImageFormat Format = imageFile.RawFormat;
bmpOut = new Bitmap(width, height, PixelFormat.Format64bppPArgb);
bmpOut.SetResolution(imageFile.HorizontalResolution,
imageFile.VerticalResolution);
bmpOut.Palette = imageFile.Palette;
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, width, height);
g.DrawImage(imageFile, 0, 0, width, height);
imageFile.Dispose();
g.Dispose();
return bmpOut;
}
The image is then saved to disk using the following:
....
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
NewImage.Save(HttpContext.Current.Server.MapPath(newImgPath), jgpEncoder,
encoderParameters);
NewImage.Dispose();
encoderParameters.Dispose();
....
small amounts, but if you repeatedly put the same image through the
following code, the image quality slowly degrades. Can anyone spot why this
may be happening? Should it only deteriate once upon intitially going
through this procedure? The deterioration is subtle, but after a few
iterations is quite noticeable..:
private static System.Drawing.Image CopyImage(System.Drawing.Image
imageFile, int width, int height)
{
System.Drawing.Bitmap bmpOut;
ImageFormat Format = imageFile.RawFormat;
bmpOut = new Bitmap(width, height, PixelFormat.Format64bppPArgb);
bmpOut.SetResolution(imageFile.HorizontalResolution,
imageFile.VerticalResolution);
bmpOut.Palette = imageFile.Palette;
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, width, height);
g.DrawImage(imageFile, 0, 0, width, height);
imageFile.Dispose();
g.Dispose();
return bmpOut;
}
The image is then saved to disk using the following:
....
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
NewImage.Save(HttpContext.Current.Server.MapPath(newImgPath), jgpEncoder,
encoderParameters);
NewImage.Dispose();
encoderParameters.Dispose();
....