Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Image Class
Image Methods
 GetThumbnailImage Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Image..::.GetThumbnailImage Method

Returns a thumbnail for this Image.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
Visual Basic (Declaration)
Public Function GetThumbnailImage ( _
    thumbWidth As Integer, _
    thumbHeight As Integer, _
    callback As Image..::.GetThumbnailImageAbort, _
    callbackData As IntPtr _
) As Image
Visual Basic (Usage)
Dim instance As Image
Dim thumbWidth As Integer
Dim thumbHeight As Integer
Dim callback As Image..::.GetThumbnailImageAbort
Dim callbackData As IntPtr
Dim returnValue As Image

returnValue = instance.GetThumbnailImage(thumbWidth, _
    thumbHeight, callback, callbackData)
C#
public Image GetThumbnailImage(
    int thumbWidth,
    int thumbHeight,
    Image..::.GetThumbnailImageAbort callback,
    IntPtr callbackData
)
Visual C++
public:
Image^ GetThumbnailImage(
    int thumbWidth, 
    int thumbHeight, 
    Image..::.GetThumbnailImageAbort^ callback, 
    IntPtr callbackData
)
JScript
public function GetThumbnailImage(
    thumbWidth : int, 
    thumbHeight : int, 
    callback : Image..::.GetThumbnailImageAbort, 
    callbackData : IntPtr
) : Image

Parameters

thumbWidth
Type: System..::.Int32
The width, in pixels, of the requested thumbnail image.
thumbHeight
Type: System..::.Int32
The height, in pixels, of the requested thumbnail image.
callback
Type: System.Drawing..::.Image..::.GetThumbnailImageAbort
A Image..::.GetThumbnailImageAbort delegate. In GDI+ version 1.0, the delegate is not used. Even so, you must create a delegate and pass a reference to that delegate in this parameter.
callbackData
Type: System..::.IntPtr
Must be Zero.

Return Value

Type: System.Drawing..::.Image
An Image that represents the thumbnail.

If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image.

The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.

The following code example creates and displays a thumbnail image. This delegate is never called.

C#
public bool ThumbnailCallback()
 {
 return false;
 }
 public void Example_GetThumb(PaintEventArgs e)
 {
 Image.GetThumbnailImageAbort myCallback =
 new Image.GetThumbnailImageAbort(ThumbnailCallback);
 Bitmap myBitmap = new Bitmap("Climber.jpg");
 Image myThumbnail = myBitmap.GetThumbnailImage(
 40, 40, myCallback, IntPtr.Zero);
 e.Graphics.DrawImage(myThumbnail, 150, 75);
 }

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Better example      jeriley   |   Edit   |  

Since more often than not, you're going to want an image thumbnail that persists aspect ratio, I wrote up a blog post here - http://blog.rileytech.net/post.aspx?id=718af0a2-28be-4b2c-b327-e54680f5d83c and incase for whatever reason the site goes away, here's the code to maintain that aspect ratio (maybe include this in the next .net?)

//start copy from here

const int thumbSize = 100;
private void MakeMeAGoodThumbnail()
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("MyImage.jpg");
int width, height;

//these are just arbitary numbers -- if the image is larger than this, create a 100px max by 100px max thumbnail defined by the constant at the start
if (bmp.Height > 320 || bmp.Width > 240)
{
float percent = DeterminePercentageForResize(bmp.Height, bmp.Width);
float floatWidth = (float)bmp.Width * percent;
float floatHeight = (float)bmp.Height * percent;
width = Convert.ToInt32(floatWidth);
height = Convert.ToInt32(floatHeight);

}
else
{
width = bmp.Width;
height = bmp.Height;
}

System.Drawing.Image thumb = bmp.GetThumbnailImage(width, height, ThumbnailCallback, IntPtr.Zero);
thumb.Save("MyNewImage.jpg");
}

private float DeterminePercentageForResize(int height, int width)
{
int highestValue;
if (height > width)
highestValue = height;
else
highestValue = width;

float percent = (float)thumbSize / (float)highestValue;

if (percent > 1 && percent != 0)
throw new Exception("Percent cannot be greater than 1 or equal to zero");
else
return percent;
}
public bool ThumbnailCallback()
{
return false;
}

Tags What's this?: null (x) Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker