HOW TO:使用影像材質填滿圖案

更新:2007 年 11 月

您可以使用 Image 類別和 TextureBrush 類別,即可使用材質填滿一個圖案。

範例

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

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

注意事項:

在下列範例中,影像尺寸為 75×75,而橢圓形的大小為 150×250。因為影像填滿的部分比橢圓形小,橢圓形會和影像並排顯示。並排顯示是指以水平和垂直方向重複顯示影像,一直到圖案的邊界為止。如需有關並排顯示的詳細資訊,請參閱 HOW TO:使用影像並排顯示圖案

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

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

編譯程式碼

上述範例是專為與 Windows Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 Paint 事件處理常式的參數)。

請參閱

其他資源

使用筆刷填滿形狀