Groups | Blog | Home
all groups > dotnet drawing api > december 2004 >

dotnet drawing api : How to use GetThumbnailImage without embedded thumbnail?



Bob Powell [MVP]
12/21/2004 4:39:28 PM
Just create your thumbnail manually by creating an image the size of the
desired thumbnail, obtaining a Graphics object from it using
Graphics.FromImage and drawing the original image to the thumbnail at the
appropriate size.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

George Nevsky
12/21/2004 6:35:09 PM
I wish to use GetThumbnailImage method of Image object to downsize an image
but I don't want to use embedded thumbnail which can be in original image
file.



I know probably it isn't best in the world way but I like it. It works
absolutely fine for my purposes. It is very simple. It works fine with
indexed color images like GIF as well as with true color images like JPEG.
It is pretty fast. But it has one horrible thing. It is using embedded
thumbnail (if there is one) which can be ugly.

How to tell to GDI+ don't use embedded thumbnail?

R=E9my_Samulski
12/21/2004 11:37:49 PM
I use the following code to create thumbnails and to enlarge images.

Public Shared Function ResizeBitmap( _
ByVal bmpBitmap As Bitmap, _
ByVal snglBoundingWidth As Single, _
ByVal snglBoundingHeight As Single, _
Optional ByVal blnHighQuality As Boolean = False) As Bitmap
Dim snglNewWidth, snglNewHeight As Single

If snglBoundingWidth > 1 And snglBoundingHeight > 1 Then
If (bmpBitmap.Height / bmpBitmap.Width) >
(snglBoundingHeight / snglBoundingWidth) Then
snglNewHeight = Int(snglBoundingHeight)
snglNewWidth = Int((bmpBitmap.Width / bmpBitmap.Height)
* snglNewHeight)
Else
snglNewWidth = Int(snglBoundingWidth)
snglNewHeight = Int((bmpBitmap.Height /
bmpBitmap.Width) * snglNewWidth)
End If
Else
snglNewWidth = bmpBitmap.Width * snglBoundingWidth
snglNewHeight = bmpBitmap.Height * snglBoundingHeight
End If

Dim bmpNewBitmap As System.Drawing.Bitmap

bmpNewBitmap = New Bitmap(bmpBitmap, snglNewWidth,
snglNewHeight)

If blnHighQuality = True Then
Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)

g.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

g.DrawImage(bmpBitmap, 0, 0, bmpNewBitmap.Width,
bmpNewBitmap.Height)
End If

Return bmpNewBitmap
End Function
George Nevsky
12/22/2004 9:23:08 AM
But Graphics.FromImage doesn't work with indexed color pictures ...


[quoted text, click to view]

Bob Powell [MVP]
12/22/2004 12:34:52 PM
It's not the indexed image that you should use.

Image i=Image.FromFile(<original>);
Bitmap bm=new Bitmap(thumbXsize, thumbYsize);
Graphics g=Graphics.FromImage(bm);
g.InterpolationMode=InterpolationMode.HighQualityBilinear;
g.DrawImage(i,new Rectangle(0,0,bm.Width,bm.Height),0,0,i.Width,
i.Height,GraphicsUnit.pixel);
g.Dispose();
i.Dispose();

bm now holds a thumbnail image that you can save or display;
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

George Nevsky
12/22/2004 5:51:47 PM
Have you tried resize indexed color image (GIF) with your function?
This line:
[quoted text, click to view]
should generate exception "A Graphics object cannot be created from an image
that has an indexed pixel format."


[quoted text, click to view]

Bob Powell [MVP]
12/22/2004 9:19:36 PM
You cannot create a Graphics object from a GIF.

The resulting thumbnail will always be a high colour image best saved as a
jpeg.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Bob Powell [MVP]
12/22/2004 9:22:41 PM
You will see in both my example and Remy's that the bitmap for which the
Graphics object is obtained is a new high-colour bitmap. The bitmap creation
in Remy's example might cause problems though. You should just use new
Bitmap(snglNewWidth,snglNewHeight)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

George Nevsky
12/22/2004 10:15:17 PM
Ok. Thank you for this example but can you also help me with indexed color
image?
How to create Graphics object from GIF?

[quoted text, click to view]

R=E9my_Samulski
12/28/2004 4:00:07 AM
No problems resizing GIF's nor with the function I use in my example.
The only thing you should take into consideration is that the bitmap is
resized without any anti-aliases when performing this code:
ResizedBitmap = new Bitmap(OriginalBitmap, snglNewWidth, snglNewHeight)
Furthermore my code is not speed optimised =) (as always).

[quoted text, click to view]
AddThis Social Bookmark Button