Udostępnij za pośrednictwem


How to: Create Thumbnail Images

A thumbnail image is a small version of an image. You can create a thumbnail image by calling the GetThumbnailImage method of an Image object.

Example

The following example constructs an Image object from a JPG file. The original image has a width of 640 pixels and a height of 479 pixels. The code creates a thumbnail image that has a width of 100 pixels and a height of 100 pixels.

The following illustration shows the thumbnail image.

Thumbnail Image

Note

In this example, a callback method is declared, but never used. This supports all versions of GDI+.

    Public Function ThumbnailCallback() As Boolean 
            Return True 
    End Function 

    Private Sub GetThumbnail(ByVal e As PaintEventArgs) 

            Dim callback As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback) 
            Dim image As Image = New Bitmap("c:\FakePhoto.jpg") 
            Dim pThumbnail As Image = image.GetThumbnailImage(100, 100, callback, New IntPtr()) 
            e.Graphics.DrawImage(pThumbnail, 10, 10, pThumbnail.Width, pThumbnail.Height) 
    End Sub 
    public bool ThumbnailCallback() {
            return true;
        }

        private void GetThumbnail(PaintEventArgs e)
        {

            Image.GetThumbnailImageAbort callback = 
                new Image.GetThumbnailImageAbort(ThumbnailCallback);
            Image image = new Bitmap(@"c:\FakePhoto.jpg");
            Image pThumbnail = image.GetThumbnailImage(100, 100, callback, new
               IntPtr());
            e.Graphics.DrawImage(
               pThumbnail,
               10,
               10,
               pThumbnail.Width,
               pThumbnail.Height);
        }
      

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. To run the example, follow these steps:

  1. Create a new Windows Forms application.

  2. Add the example code to the form.

  3. Create a handler for the form's Paint event

  4. In the Paint handler, call the GetThumbnail method and pass e for PaintEventArgs.

  5. Find an image file that you want to make a thumbnail of.

  6. In the GetThumbnail method, specify the path and file name to your image.

  7. Press F5 to run the example.

    A 100 by 100 thumbnail image appears on the form.

See Also

Other Resources

Images, Bitmaps, and Metafiles

Working with Images, Bitmaps, Icons, and Metafiles