png image from ShellFile (Windows)

Hi Experts,

If I have the full path to a file in Windows, how can I create a png image out of its thumbnail with a specific width and height?

ShellFile shellFile = ShellFile.FromFilePath("c:\\test\\myfile.txt");
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

That code is fine to produce an extra large bitmap, but I need a png of say, 100x100 (or any size)...

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Try this.

Scaling code. The most difficult part of generating thumbnails is probably scaling them. It isn't a good idea just to decrease all dimensions by 50% if the images are of different sizes, because then some thumbnails will be much larger than others.

The scaling logic here caps each dimension to 40 pixels. Based on the larger dimension, it scales the entire image.

I tested this code with different sizes of images, both horizontally larger and vertically larger, and it worked correctly.

using System;
using System.Drawing;

class Program
    static void Main()
	const string input = "C:\\background1.png";
	const string output = "C:\\thumbnail.png";

	// Load image.
	Image image = Image.FromFile(input);

	// Compute thumbnail size.
	Size thumbnailSize = GetThumbnailSize(image);

	// Get thumbnail.
	Image thumbnail = image.GetThumbnailImage(thumbnailSize.Width,
	    thumbnailSize.Height, null, IntPtr.Zero);

	// Save thumbnail.

    static Size GetThumbnailSize(Image original)
	// Maximum size of any dimension.
	const int maxPixels = 40;

	// Width and height.
	int originalWidth = original.Width;
	int originalHeight = original.Height;

	// Compute best factor to scale entire image based on larger dimension.
	double factor;
	if (originalWidth > originalHeight)
	    factor = (double)maxPixels / originalWidth;
	    factor = (double)maxPixels / originalHeight;

	// Return thumbnail size.
	return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
Awesome, thanks Randy!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.