Better thumbnail creation then "GetThumbnailImage" in C sharp
2/21/2010 03:03:00 PM
Posted by RIAlog
In many cases you may want to create a smaller version of your images. maybe for mobile devices or for images view on your page.
One known way is to use the "ready-made" GetThumbnailImage function. That will generally do the job (another way is below):
int Width; int Height; int ThumbWidth; int ThumbHeight; string SavePath = "[Some save path"]; string FilenameWOext = "[]"; string FileExtention; Bitmap btmp1 = new Bitmap(SavePath + FilenameWOext + FileExtention); Width = btmp1.Width; Height = btmp1.Height; System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); if (ThumbWidth > Width && ThumbHeight > Height) { ThumbWidth = Width; ThumbHeight = Height; } System.Drawing.Image myThumbnail1 = btmp1.GetThumbnailImage(ThumbWidth, ThumbHeight, myCallBack, IntPtr.Zero); myThumbnail1.Save(SavePath + ThumbFile); btmp1.Dispose();
HOWEVER, this will sometimes wont work. when using images from some digital cameras, there is addotional EXIF data that makes the generated image to be blur.
Fortunatly, there is another way using Graphics.DrawImage
Bitmap resized = new Bitmap(ThumbWidth, ThumbHeight); Graphics g = Graphics.FromImage(resized); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(btmp1, new Rectangle(0, 0, resized.Width, resized.Height)); g.Dispose();
This way will work with EXIF images or any other image with high quality too!
Enjoy!
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
. Follow any responses to this post through RSS. You can leave a response, or trackback from your own site.
Subscribe to:
Post Comments (Atom)
Post a Comment