如何:建立縮圖影像
縮圖影像是映像的小型版本。 您可以呼叫 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 。 若要執行範例,請遵循下列步驟:
建立新的 Windows Form 應用程式。
將範例程式代碼新增至表單。
建立表單 Paint 事件的處理程式
在處理程式中Paint,呼叫
GetThumbnail
方法並傳遞e
。PaintEventArgs尋找您想要建立縮圖的影像檔。
在方法中
GetThumbnail
,指定映像的路徑和檔名。按 F5 執行範例。
表單上會出現 100 到 100 個縮圖影像。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應