Re: Problem with reading JPEG images in .NET -- WORKING SOLUTION
- From: Nick Gilbert <nickg@xxxxxxxxxxxxxxxx>
- Date: Thu, 26 Nov 2009 15:00:37 +0000
Alexey Smirnov wrote:
On Nov 26, 11:24 am, Nick Gilbert <ni...@xxxxxxxxxxxxxxxx> wrote:Jani Järvinen [MVP] wrote:Hello Nick,I have found the cause of the problem. I'm using GetThumbnailImage()I have noticed that when reading some images into .NET, sometimes theyI wrote a very simple WinForms application with a button and two
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.
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.
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.
.
- References:
- Problem with reading JPEG images in .NET
- From: Nick Gilbert
- Re: Problem with reading JPEG images in .NET
- From: Jani Järvinen [MVP]
- Re: Problem with reading JPEG images in .NET
- From: Nick Gilbert
- Re: Problem with reading JPEG images in .NET
- From: Alexey Smirnov
- Problem with reading JPEG images in .NET
- Prev by Date: Re: Two column array
- Next by Date: Re: IE 7.0 outer div height issue
- Previous by thread: Re: Problem with reading JPEG images in .NET
- Next by thread: Re: Problem with reading JPEG images in .NET
- Index(es):
Relevant Pages
|