TextureBrush对象用于基于光栅的图像来填充图形。它使用一个来自图像文件如.bmp、.jpg或.png的图像。使用Bitmap类可以从文件中获取图像,Bitmap类时Image类的一个子类,为此,可以使用如下代码用图案填充:
1
private
void
Form1_Paint(
object
sender, PaintEventArgs e)
2 {
3 Graphics g = e.Graphics;
4 Bitmap bmp = new Bitmap( " D:\\Images\\alphabet.gif " );
5 TextureBrush tb = new TextureBrush(bmp);
6
7 g.FillRectangle(tb, 20 , 20 , 200 , 70 );
8 bmp.Dispose();
9 tb.Dispose();
10 }
2 {
3 Graphics g = e.Graphics;
4 Bitmap bmp = new Bitmap( " D:\\Images\\alphabet.gif " );
5 TextureBrush tb = new TextureBrush(bmp);
6
7 g.FillRectangle(tb, 20 , 20 , 200 , 70 );
8 bmp.Dispose();
9 tb.Dispose();
10 }
得到的图像为:
把图像中的一个选区作为平铺图片,重载TextureBrush构造函数为允许选择图像的一部分用作TextureBrush填充图形是的平铺图片。如:
1
private
void
Form1_Paint(
object
sender, PaintEventArgs e)
2 {
3 Graphics g = e.Graphics;
4 Bitmap bmp = new Bitmap( " D:\\Images\\alphabet.gif " );
5 TextureBrush tb = new TextureBrush(bmp, new Rectangle( 0 , 0 , 25 , 25 ));
6
7 g.FillRectangle(tb, 20 , 20 , 200 , 70 );
8 g.FillRectangle(tb, 45 , 45 , 70 , 150 );
9 bmp.Dispose();
10 tb.Dispose();
11 }
2 {
3 Graphics g = e.Graphics;
4 Bitmap bmp = new Bitmap( " D:\\Images\\alphabet.gif " );
5 TextureBrush tb = new TextureBrush(bmp, new Rectangle( 0 , 0 , 25 , 25 ));
6
7 g.FillRectangle(tb, 20 , 20 , 200 , 70 );
8 g.FillRectangle(tb, 45 , 45 , 70 , 150 );
9 bmp.Dispose();
10 tb.Dispose();
11 }
图像为: