作法:建立縮圖影像
縮圖影像是小型的影像版本。 您可以呼叫 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 Form 使用而設計,且其需要 PaintEventArgse
,這是 Paint 事件處理常式的參數。 若要執行範例,請遵循下列步驟:
建立新的 Windows Form 應用程式。
將範例程式碼新增至表單。
建立表單 Paint 事件的處理常式
在處理常式中 Paint
GetThumbnail
,呼叫 方法並針對 PaintEventArgs 傳遞e
。尋找您想要建立縮圖的影像檔。
在 方法中
GetThumbnail
,指定映射的路徑和檔案名。按 F5 執行範例。
表單上會出現 100 個 100 個縮圖影像。