共用方式為


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

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

範例

下列範例會以影像填滿橢圓形。 程式碼會 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 Form 使用而設計,且其需要 PaintEventArgse,這是 Paint 事件處理常式的參數。

另請參閱