Image quality

Discussion in 'ASP .Net' started by JJ, Jul 3, 2008.

  1. JJ

    JJ Guest

    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();

    ....
     
    JJ, Jul 3, 2008
    #1
    1. Advertising

  2. On Jul 3, 4:38 pm, "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..:
    >
    > 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();
    >
    > ...


    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);
     
    Alexey Smirnov, Jul 3, 2008
    #2
    1. Advertising

  3. JJ

    JJ Guest

    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" <> wrote in message
    news:...
    On Jul 3, 4:38 pm, "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..:
    >
    > 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();
    >
    > ...


    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);
     
    JJ, Jul 3, 2008
    #3
  4. On Jul 3, 9:38 pm, "JJ" <> wrote:
    > 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" <> wrote in message
    >
    > news:...
    > On Jul 3, 4:38 pm, "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..:

    >
    > > 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();

    >
    > > ...

    >
    > 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);
     
    Alexey Smirnov, Jul 3, 2008
    #4
  5. On Jul 3, 4:38 pm, "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..:
    >
    > 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;


    In addition, you can try to use SmoothingMode.HighQuality and
    InterpolationMode.Bilinear (I use often this one). Hope this helps.
     
    Alexey Smirnov, Jul 3, 2008
    #5
  6. JJ

    JJ Guest

    Thanks I'll give it a go.
    JJ
    "Alexey Smirnov" <> wrote in message
    > In addition, you can try to use SmoothingMode.HighQuality and
    > InterpolationMode.Bilinear (I use often this one). Hope this helps.
     
    JJ, Jul 4, 2008
    #6
  7. 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
     
    Göran Andersson, Jul 4, 2008
    #7
  8. JJ

    JJ Guest

    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" <> wrote in message
    news:...
    > 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
     
    JJ, Jul 5, 2008
    #8
  9. 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
     
    Göran Andersson, Jul 5, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris D

    Thumbnail Image Quality Issues

    Chris D, Nov 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    352
    Chris D
    Nov 18, 2003
  2. Chris D

    Thumbnail Image Quality Issues

    Chris D, Nov 19, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    1,712
  3. neverstill

    resizing image has terrible quality

    neverstill, Dec 17, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    495
    neverstill
    Dec 17, 2003
  4. =?Utf-8?B?a2Zpcg==?=

    image quality on Image control

    =?Utf-8?B?a2Zpcg==?=, Dec 14, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    =?Utf-8?B?a2Zpcg==?=
    Dec 14, 2005
  5. Daniel Mark
    Replies:
    1
    Views:
    792
    Fredrik Lundh
    Sep 25, 2006
Loading...

Share This Page