如何:创建缩略图像

缩略图像是图像的小版本。 可通过调用 Image 对象的 GetThumbnailImage 方法创建缩略图像。

示例

下面的示例从 JPG 文件构造 Image 对象。 原始图像的宽度为 640 像素,高度为 479 像素。 该代码创建宽度和高度均为 100 像素的缩略图像。

下面的插图显示此缩略图像。

缩略图像

提示

此示例中声明了一个回调方法,但从未用到此方法。 这支持 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);
        }
      

编译代码

前面的示例是为使用 Windows 窗体而设计的,它需要 Paint 事件处理程序的参数 PaintEventArgs e。 若要运行此示例,请按以下步骤进行操作:

  1. 创建新的 Windows 窗体应用程序。

  2. 将代码示例添加到窗体中。

  3. 为窗体的 Paint 事件创建一个处理程序。

  4. Paint 处理程序中,调用 GetThumbnail 方法并为 PaintEventArgs 传递 e。

  5. 找到要生成其缩略图的图像文件。

  6. 在 GetThumbnail 方法中,指定图像的路径和文件名。

  7. 按 F5 运行示例。

    一个 100 x 100 的缩略图图像出现在窗体上。

请参见

其他资源

图像、位图和图元文件

使用图像、位图、图标和图元文件