473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Image quality

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.HorizontalResolutio n,
imageFile.VerticalResolution);

bmpOut.Palette = imageFile.Palette;

Graphics g = Graphics.FromImage(bmpOut);

g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQu ality;
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(n ewImgPath), jgpEncoder,
encoderParameters);

NewImage.Dispose();

encoderParameters.Dispose();

....
Jul 3 '08 #1
8 2362
On Jul 3, 4:38*pm, "JJ" <a...@xyz.comwrote:
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.HorizontalResolutio n,
imageFile.VerticalResolution);

bmpOut.Palette = imageFile.Palette;

Graphics g = Graphics.FromImage(bmpOut);

g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQu ality;

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(n ewImgPath), jgpEncoder,
encoderParameters);

NewImage.Dispose();

encoderParameters.Dispose();

...
Well, I think it depends on the original image. You should try to play
with the values to find better result. For example, I am always adding
an additional value to the EncoderParameter for LZW Compression, which
is basically a technique for lossless compression:

encoderParameters.Param[1] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionLZW);
Jul 3 '08 #2
JJ
Thanks Alexey.

I did just try that and it came up with an exception, but I'll looking a
little deeper into the 'encoder' area.
Thanks for the pointer,
JJ
"Alexey Smirnov" <al************@gmail.comwrote in message
news:b0**********************************@c65g2000 hsa.googlegroups.com...
On Jul 3, 4:38 pm, "JJ" <a...@xyz.comwrote:
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.HorizontalResolutio n,
imageFile.VerticalResolution);

bmpOut.Palette = imageFile.Palette;

Graphics g = Graphics.FromImage(bmpOut);

g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQu ality;

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(n ewImgPath), jgpEncoder,
encoderParameters);

NewImage.Dispose();

encoderParameters.Dispose();

...
Well, I think it depends on the original image. You should try to play
with the values to find better result. For example, I am always adding
an additional value to the EncoderParameter for LZW Compression, which
is basically a technique for lossless compression:

encoderParameters.Param[1] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionLZW);
Jul 3 '08 #3
On Jul 3, 9:38*pm, "JJ" <a...@xyz.comwrote:
Thanks Alexey.

I did just try that and it came up with an exception, but I'll looking a
little deeper into the 'encoder' area.
Thanks for the pointer,
JJ"Alexey Smirnov" <alexey.smir...@gmail.comwrote in message

news:b0**********************************@c65g2000 hsa.googlegroups.com...
On Jul 3, 4:38 pm, "JJ" <a...@xyz.comwrote:


Whilst I am resizing images I am losing quality. This is only happeningin
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.HorizontalResolutio n,
imageFile.VerticalResolution);
bmpOut.Palette = imageFile.Palette;
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;
g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQu ality;
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(n ewImgPath), jgpEncoder,
encoderParameters);
NewImage.Dispose();
encoderParameters.Dispose();
...

Well, I think it depends on the original image. You should try to play
with the values to find better result. For example, I am always adding
an additional value to the EncoderParameter for LZW Compression, which
is basically a technique for lossless compression:

encoderParameters.Param[1] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionLZW);- Hide quoted text -

- Show quoted text -
hmm... what kind of exception, General GDI+ Error?
Note, that you would need to resize an array, because now you would
have two values.

encoderParameters = new EncoderParameters(2);
Jul 3 '08 #4
On Jul 3, 4:38*pm, "JJ" <a...@xyz.comwrote:
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.HorizontalResolutio n,
imageFile.VerticalResolution);

bmpOut.Palette = imageFile.Palette;

Graphics g = Graphics.FromImage(bmpOut);

g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuali ty;

g.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighQu ality;
In addition, you can try to use SmoothingMode.HighQuality and
InterpolationMode.Bilinear (I use often this one). Hope this helps.
Jul 3 '08 #5
JJ
Thanks I'll give it a go.
JJ
"Alexey Smirnov" <al************@gmail.comwrote in message
In addition, you can try to use SmoothingMode.HighQuality and
InterpolationMode.Bilinear (I use often this one). Hope this helps.

Jul 4 '08 #6
JJ wrote:
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..:
If you resize an image an compress it with JPEG, you will get a quality
loss every time.

If you don't resize the image, and recompress it with JPEG, you will not
loose much data, as most of the data that the compression throws away
is data that was recreated when the image was decompressed. If you
resize the image, you will use both the original data and recreated data
to create a new image. When you compress this new image, the data that
the compression throws away doesn't correspond with the data that came
from the recreated data, so you are throwing away more and more of the
original data every time.

For example, the compression throws away 75% of the color information,
because color is less important than intensity. It calculates the
avarage color for each four pixels in a 2x2 grid, and uses the average
color for all four of the pixels. When you recompress the image, the
four pixels already have the same color, so no actual color information
is lost. If you resize the image, the 2x2 grid no longer matches the
loaded data, so the compression again throws away some of the color
information.

--
Göran Andersson
_____
http://www.guffa.com
Jul 4 '08 #7
JJ
Thanks Göran.

By saving it as jpeg at 100L quality setting, is this compressing it
further? I am not, in effect, resizing after the first pass, as the
dimensions do not change (though I am resaving it - which I've now worked
around).
The loss is small and only obviouse when the image has fine detail (like
white text on dark backgrounds).

In this case, I would be interested in how you can make an exact copy of an
image and save to the same file. The clone() method is one option I suppose,
but there are issues there if you are saving back to the same file (GDI+
errors). The popular solution seems to be creating a new graphics object
from the original then disposing, as I try to do - but then I can't work
around this gradual loss.

Thanks,
JJ


"Göran Andersson" <gu***@guffa.comwrote in message
news:Og**************@TK2MSFTNGP03.phx.gbl...
JJ wrote:
>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..:

If you resize an image an compress it with JPEG, you will get a quality
loss every time.

If you don't resize the image, and recompress it with JPEG, you will not
loose much data, as most of the data that the compression throws away is
data that was recreated when the image was decompressed. If you resize the
image, you will use both the original data and recreated data to create a
new image. When you compress this new image, the data that the compression
throws away doesn't correspond with the data that came from the recreated
data, so you are throwing away more and more of the original data every
time.

For example, the compression throws away 75% of the color information,
because color is less important than intensity. It calculates the avarage
color for each four pixels in a 2x2 grid, and uses the average color for
all four of the pixels. When you recompress the image, the four pixels
already have the same color, so no actual color information is lost. If
you resize the image, the 2x2 grid no longer matches the loaded data, so
the compression again throws away some of the color information.

--
Göran Andersson
_____
http://www.guffa.com

Jul 5 '08 #8
JJ wrote:
Thanks Göran.

By saving it as jpeg at 100L quality setting, is this compressing it
further?
Even at the quality setting 100, there is still some compression and
some quality loss. For one, it still throws away 75% of the color
information, as I described.
I am not, in effect, resizing after the first pass, as the
dimensions do not change (though I am resaving it - which I've now worked
around).
The loss is small and only obviouse when the image has fine detail (like
white text on dark backgrounds).
Yes, if you don't resize the image, the degradation should be minimal.
In this case, I would be interested in how you can make an exact copy of an
image and save to the same file. The clone() method is one option I suppose,
but there are issues there if you are saving back to the same file (GDI+
errors). The popular solution seems to be creating a new graphics object
from the original then disposing, as I try to do - but then I can't work
around this gradual loss.
If you load the image from a file, you have to dispose that image before
you can save anything to that file. One way to get around that is to
open a FileStream for reading the file, and load the image from that
stream. That way you can dispose the stream so that the image is not
keeping the file open.

The Bitmap class has a constructor that takes an Image, that would be
the most straight forward way of making a copy of an image.

--
Göran Andersson
_____
http://www.guffa.com
Jul 5 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sean | last post by:
i am writing a class that visual demonstrates the result of changing the quality of a jpeg. the intention is to do this by saving the file and a selected quality, then reloading it to get an idea of...
5
by: Popoxinhxan | last post by:
HI guy I am working on the web application that required to display the chart image on the page. The process is clicking the button and browser will popup another windows that displayed...
6
by: Chris D | last post by:
Hi, I have an application where a user uploads an image and I create two thumbnails. One a small image and the second is a larger image but still smaller then the original. I store these in SQL...
6
by: neverstill | last post by:
hi- So I wrote this nice little page that will allow the managers to add images to the products table. Without too many details to confuse everything, basically what I'm doing is: getting an...
7
by: somequestion | last post by:
System.Drawing.Image newImage = Bitmap(....) newImage.Save(destinationPath, ImageFormat.Jpeg); this image resolution is very low i'd like to make high quality image so i change...
1
by: Daniel Mark | last post by:
hello all: I am using Image module from PIL to save created image as JPG format. Is there any option I could set for function Image.save so that the stored image will have the highest quality....
9
by: kombu67 | last post by:
I'm reading a series of images from a MS SQL table and saving them to directory. These are staff ID pictures from our security card app. Once I've extracted the ID photo from the security app to...
11
by: shapper | last post by:
Hello, I am displaying an image on a few pages. The image size is 50 px height and 50 px width. In some pages I need the image to be 30x30 px in others 40x40 px and in others 50x50px. Can I...
1
by: Sinan Alkan | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.