Note: This blog has been moved to http://blog.yuvisense.net

Saturday, April 15, 2006

[Tech]Creating thumbnails of an Image in just a single Line...

Well, Thumbnails were all the rage during the dot-com era, during which I had no idea of the Internet. And, they were all useful in the good ol' [though for me non-existent] Dialup Days. Though they're of less importance now due to the explosion of Broadband, thumbnails are still useful. So, here's a simple one line of code that would convert any Image of practically any widely used Format into a Thumbnail of a given size into many widely used Image Formats.

        System.Drawing.Image.FromFile _
             ("c:\MyPicture.png").GetThumbnailImage _
             (150, 150, Nothing, System.IntPtr.Zero).Save _
             ("C:\Thumbnail.gif", System.Drawing.Imaging.ImageFormat.Gif)

OK, that's not one Physical Line, but it is one Logical Line, so that 1 Line Claim still holds:D OK, let's see what we're doing here... First up, we are using the FromFile static Method of the System.Drawing.Image class to load an Image from disk and return an Image Object, with which we immediately call GetThumbnailImage, with the width and height of the Thumbnail, along with a dubious Nothing [Null for C#ers] and a Zero Pointer. I donno why they are there, but the docs said that though they weren't used, they had to be there. Donno why. Poor Framework Design:P Anyway, the GetThumbnailImage call returns yet another Image object, on which we call the Save Method with the File Name and the Image Format of the Thumbnail file. Intristically, .NET supports 10 Image Formats. List of them found here BTW, I made the Code a bit Unreadable using line continuation, inorder for that 1 line Title:D Sorry about that. And, Where are the comments ?

4 Comments:

Blogger Aswin Anand T.H. said...

You always come up with some interesting bits of info :D...very nice!

4/15/2006 01:36:00 PM  
Blogger Aswin Anand T.H. said...

Dude, ur blog doesnt render properly in IE6

4/16/2006 02:23:00 AM  
Blogger Yuvi Panda said...

Yea I know. Ramesh told me already about it. Problem is, I don't have IE6 to test it on! I was planning to use a VM to test it on, but since Dad is delaying my hardware upgrade, I've to wait some time. Sorry IE6 People.....

4/16/2006 02:25:00 AM  
Anonymous Anonymous said...

Very nice! I found a place where you can
make some nice extra cash secret shopping. Just go to the site below
and put in your zip to see what's available in your area.
I made over $900 last month having fun!
make extra money

4/21/2006 03:01:00 AM  

Post a Comment

<< Home