smithvoice.com
 Y'herd thisun? 

“You can export from and to any data format... once you know the middleware trick.”

from this classic page by smith

Funky thumbnail images?

TaggedCoding, Imaging

I've got a program that has to show image thumbnails. Everyone says to use GetThumbnailImage and it works great for some of the jpgs but others look terrible. They're all rippled around the edges of the different objects in the pictures. When I test using a regular picturebox with SizeMode = StretchImage and with the width and height of the control manually set to my thumbnail dimensions all of the images look fine.

Do the images that look wonky come from a digital camera?

I don't know, maybe. How can I tell?

Right click on one of the odd images, click on "Open with..." and choose Notepad from the list. Don't tell Windows to use Notepad for the file type, just use it for this test.

When the file is displayed, look at the top for a mention of "EXIF" and look around the header area for a mention of a camera company and/or a date stamp. If you see that kind of stuff then the file came directly from a digital camera.

Now do the same thing on a file that looks good using GetThumbnailImage. In the header you're probably going to see "JFIF" pretty close to the top and none of that company/timestamp stuff.

The ones that don't work all have the "Exif". But so what? They're all JPEGs.

Yeah, but most cameras add that "Exchangeable Image File" header and part of that header is an embedded thumbnail. The thumbnail is used to make the tiny images you see on the camera screen; it's super-compressed and if you blow it up it tends to look bad. Folks who got in the habit of using GetThumbnailImage all the time are taking advantage of a side-effect of the method. The call Gets the thumbnail image and only falls back on resizing the main image. So, on one of these files, when you think you're reducing your source image, you're really enlarging the tiny thumbnail and enlarging without good resampling never quite looks right.

There's a note about this in the "Remarks" section of the GetThumbNailImage docs, it just doesn't go the extra step to tell you that those embedded thumbnails are in most all digital camera files.

As that page says, it's best to stick to DrawImage to create a new image (or file) instead of GetThumbnailImage. DrawImage isn't hard at all, it just takes a few more lines.

Note: After you've used a version of DrawImage to get rid of the inner thumbnail, GetThumbnailImage is fine for quickies, such as fast resizing for ASP.Net displays where setting sizes with HTML img width/height properties isn't convenient or where server permissions might not allow the lower-level DrawImage. Keep in mind however that GetThumbnailImage alone on a full sized image may not do all that much to reduce file/memory sizes and download times so, if possible, create your thumbnails up front as files using DrawImage rather than resizing images from scratch on the server for every page refresh.

We putĀ our aspect-correct resize code on smithvoice.com for you and you can go ahead and use it but also take some time to get a handle on DrawImage because it has a lot of power beyond just this need.

One more question. What's the best way to display an unknown number of thumbnails? I've been trying to use the ListView because that's what it looks like Windows uses, but it looks like I have to add all the images to an ImageList first.

You can dynamically add images to an ImageList, it works if you have nothing else. You can also look to Rod Stephens' classic VB Graphics book and bring his excellent example up to VB7.

Related information

Lighten/Darken without P/Invoke and SetPixelV by using a fast color matrix: Thanks to Michael Combs

Adjust contrast with a helpful color matrix fudge: Thanks to Bob Powell

If bicubic interpolation alone isn't giving you high enough quality on your gif conversions, take advantage of C#'s Unsafe breaking the .Net CLS pointer rules: Thanks to Brendan Tompkins (look in the feedback for a VB.net enhancement for setting transparency)

Hope it helps!

Robert Smith
Kirkland, WA



home     who is smith    contact smith     rss feed π
Since 1997 a place for my stuff, and it if helps you too then all the better smithvoice.com