Re: Problem with reading JPEG images in .NET -- WORKING SOLUTION

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance





Alexey Smirnov wrote:
On Nov 26, 11:24 am, Nick Gilbert <ni...@xxxxxxxxxxxxxxxx> wrote:
Jani Järvinen [MVP] wrote:
Hello Nick,
I have noticed that when reading some images into .NET, sometimes they
load in an extremely low quality (like they've been resized up from a
thumbnail or icon size). ... [snip]
Sample A loads in looking extremely blurred, despite the fact that looks
identical to the other one when viewed in any browser or Adobe Photoshop.
I wrote a very simple WinForms application with a button and two
PictureBoxes on a form. For the code, I wrote:
---------------------------
private void button1_Click(object sender, EventArgs e)
{
Bitmap a = new Bitmap("C:\\samplea.jpg");
Bitmap b = new Bitmap("C:\\sampleb.jpg");
pictureBox1.Image = a;
pictureBox2.Image = b;
}
---------------------------
With this test at least, the both of images look fine to me. You are using
the Bitmap class in an ASP.NET application, how are you loading the images?
Are you doing any processing?
Adobe Photoshop likely adds an ICC profile and possibly a thumbnail to the
JPEGs. Also, have you checked that the color profile is sRGB and note Adobe
RGB? Try "Export To Web" from Photoshop to see if that makes any difference.
I have found the cause of the problem. I'm using GetThumbnailImage()

Apparently this sometimes finds a low quality embedded thumbnail inside
the JPEG, and will always use that instead of the real image. This means
this function is useless for resizing images to anything other than a
very small thumbnail smaller than 100x100. I will replace the code with
some code which doesn't call this method.

Thanks,
Nick...- Hide quoted text -

- Show quoted text -

Yup. MSDN says: GetThumbnailImage works well when the requested
thumbnail image has a size of about 120 x 120. If you request a large
thumbnail image (say 300 x 300) from an Image object that has an
embedded thumbnail, there could be a noticeable loss of quality in the
thumbnail image. It might be better to scale the main image (instead
of scaling the embedded thumbnail) by calling DrawImage.

http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage(VS.71).aspx


OK, I tried DrawImage but it then produced a high quality thumbnail which had a black or grey border on the edges. Many online samples crop out the border, but to me, this is a hack. Eventually I found a method to produce high quality resized images of any size, which do not have any kind of border visible. Code below for completeness:

Bitmap bitmap =
new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
Graphics g = Graphics.FromImage(bitmap);
g.CompositingMode = CompositingMode.SourceCopy;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// NMG: the following 2 lines ensures GDI+ has enough pixels to work
// with when interpolating near edges
ImageAttributes ia = new ImageAttributes();
ia.SetWrapMode(WrapMode.TileFlipXY);
g.DrawImage(m_src_image,
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
0, 0, m_src_image.Width, m_src_image.Height,
GraphicsUnit.Pixel, ia);

The important it here is the ImageAttributes. Some people reported good results with the CompositingMode and PixelOffsetMode lines, but I found that although these reduced the border, the border was still visible in images with a very light, or white background.

Fundamentally, the code above does not use any embedded thumbnail image, so it can be used to resize an image to any size in good quality.

Nick.
.



Relevant Pages

  • Re: Slow Pages for Dial-up
    ... thumbnail, and this way he is prepared to wait for the image to download. ... > play with the quality of them a bit to get a good ratio of quality/size. ... rollover images probably is not a good idea for you. ... >> You can see the site needs photos of the projects but if someone has ...
    (microsoft.public.frontpage.client)
  • Re: Convert a monochrome (1bit) image into a grayscale (8bit) one
    ... > during faxing. ... >>image to increase the quality when I reseize the image. ... I can't read anything on the thumbnail. ... do this with JAI... ...
    (comp.lang.java.programmer)
  • Re: Im loosing my pixels!
    ... with good quality, FWIW ... ... so this 25-30 KB thumbnail flow appears to ... I'm glad you checked the files sizes. ...
    (comp.graphics.apps.photoshop)
  • Re: Thumbnails
    ... TweakUI for Windows XP SP1 provides an option using which you can alter the default size and quality of thumbnail images. ... Download and install TweakUI from here ...
    (microsoft.public.windowsxp.general)
  • Re: Brain dead... write image to file
    ... function resize(){ ... use the SECOND parameter to ... $origStream = imagecreatefromstring; ... // calculate thumbnail size, retaining aspect ratio ...
    (php.general)