共用方式為


如何:建立縮圖影像

縮圖影像是映像的小型版本。 您可以呼叫 GetThumbnailImage 物件的 方法 Image 來建立縮圖影像。

範例

下列範例會 Image 從 JPG 檔案建構 物件。 原始影像的寬度為 640 像素,高度為 479 像素。 此程式代碼會建立寬度為 100 像素且高度為 100 像素的縮圖影像。

下圖顯示縮圖影像:

顯示輸出縮圖的螢幕快照。

注意

在此範例中,會宣告回呼方法,但從未使用過。 這支援所有 GDI+版本。

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);
}
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 

編譯程式碼

上述範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgs e,這是事件處理程式的參數 Paint 。 若要執行範例,請遵循下列步驟:

  1. 建立新的 Windows Form 應用程式。

  2. 將範例程式代碼新增至表單。

  3. 建立表單 Paint 事件的處理程式

  4. 在處理程式中Paint,呼叫 GetThumbnail 方法並傳遞 ePaintEventArgs

  5. 尋找您想要建立縮圖的影像檔。

  6. 在方法中 GetThumbnail ,指定映像的路徑和檔名。

  7. 按 F5 執行範例。

    表單上會出現 100 到 100 個縮圖影像。

另請參閱