다음을 통해 공유


Filling a Shape with an Image Texture

You can fill a closed shape with a texture by using the Image class and the TextureBrush class.

The following example fills an ellipse with an image. The code constructs an Image object, and then passes the address of that Image object as an argument to a TextureBrush constructor. The third statement scales the image, and the fourth statement fills the ellipse with repeated copies of the scaled image.

Dim image = New Bitmap("ImageFile.jpg")
Dim tBrush As New TextureBrush(image)
tBrush.Transform = New Matrix( _
   75F / 640F, _
   0F, _
   0F, _
   75F / 480F, _
   0F, _
   0F)
e.Graphics.FillEllipse(tBrush, New Rectangle(0, 150, 150, 250))
[C#]
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));

In the preceding code, the Transform property contains the transformation that is applied to the image before it is drawn. Assume that the original image has a width of 640 pixels and a height of 480 pixels. The transform shrinks the image to 75×75 by setting the horizontal and vertical scaling values.

Note   In the preceding example, the image size is 75×75, and the ellipse size is 150×250. Because the image is smaller than the ellipse it is filling, the ellipse is tiled with the image. Tiling means that the image is repeated horizontally and vertically until the boundary of the shape is reached. For more information about tiling, see Tiling a Shape with an Image.