如何:用图像纹理填充形状
通过使用 Image 类和 TextureBrush 类,可用纹理填充闭合的形状。
示例
下面的示例用图像填充椭圆。 该代码构造 Image 对象,然后将该 Image 对象的地址作为参数传递给 TextureBrush 构造函数。 第三条语句缩放图像,第四条语句用缩放后图像的重复副本填充椭圆。
在下面的代码中,Transform 属性包含在绘制图像之前应用到该图像的转换。 假定原始图像的宽度为 640 像素,高度为 480 像素。 该转换通过设置水平和垂直缩放值将图像缩小到 75 x 75。
提示
在下面的示例中,图像大小为 75 x 75、椭圆大小为 150 x 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))
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 窗体而设计的,它需要 Paint 事件处理程序的参数 PaintEventArgs e。