共用方式為


如何:使用影像材質填滿圖案

您可以使用 Image 類別和 TextureBrush 類別,以紋理填滿封閉圖形。

範例

下列範例會使用影像填滿橢圓形。 程式碼會建構 Image 物件,然後將該 Image 物件的位址作為引數傳遞至 TextureBrush 建構函式。 第三個陳述式會縮放影像,而第四個陳述式會填入橢圓形,並填入已縮放影像的重複複本。

在下列程式碼中,Transform 屬性包含在繪製影像之前套用至影像的轉換。 假設原始影像的寬度為 640 像素,高度為 480 像素。 轉換會藉由設定水平和垂直縮放值,將影像壓縮為 75×75。

注意

在下列範例中,影像大小為 75×75,而橢圓形大小為 150×250。 由於影像小於其填滿的橢圓形,因此橢圓形會以影像並排顯示。 並排表示影像會水平和垂直重複,直到觸及圖形的邊界為止。 如需並排的詳細資訊,請參閱操作說明:使用影像來並排圖形

Image image = new Bitmap("ImageFile.jpg");
TextureBrush tBrush = new TextureBrush(image);
tBrush.Transform = new Matrix(
   75.0f / 640.0f,
   0.0f,
   0.0f,
   75.0f / 480.0f,
   0.0f,
   0.0f);
e.Graphics.FillEllipse(tBrush, new Rectangle(0, 150, 150, 250));
Dim image As New Bitmap("ImageFile.jpg")
Dim tBrush As New TextureBrush(image)
tBrush.Transform = New Matrix( _
   75.0F / 640.0F, _
   0.0F, _
   0.0F, _
   75.0F / 480.0F, _
   0.0F, _
   0.0F)
e.Graphics.FillEllipse(tBrush, New Rectangle(0, 150, 150, 250))

編譯程式碼

上述範例是為了搭配 Windows Forms 使用而設計,且其需要 PaintEventArgse,這是 Paint 事件處理常式的參數。

另請參閱