在TextureBrush 类的构造函数的参数中,WrapMode 指定了画刷中基本图案的排列方式,GDI+ 的WrapMode 枚举提供了五种不同的排列方式:
具体的应用如下图所示:
使用上一章的金鱼图片进行填充,Demo:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
Pen myPen = new Pen(Color.Blue, 3);
SolidBrush sBrush = new SolidBrush(Color.Black);
Font myFont = new Font("Arial", 13);
//装入纹理图片
Bitmap bmp = new Bitmap(@"C:\Users\ThinkPad\Desktop\GDI+(12)-纹理画刷TextureBrush的排列方式\Nemo.jpg");
//构建纹理画刷
TextureBrush tBrush = new TextureBrush(bmp);
//对画刷进行缩放,以便查看效果
Matrix matrix = new Matrix(0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f);
tBrush.Transform = matrix;
int i = 0;
//对图片不使用排列方式
tBrush.WrapMode = WrapMode.Clamp;
g.FillRectangle(tBrush, new Rectangle(i * 150, 10, 150, 150));
g.DrawRectangle(myPen, new Rectangle(i * 150, 10, 150, 150));
g.DrawString("Clamp", myFont, sBrush, new PointF(0, 170));
i += 1;
//对图片使用平铺排列方式
tBrush.WrapMode = WrapMode.Tile;
g.FillRectangle(tBrush, new Rectangle(i * 150 + 20, 10, 150, 150));
g.DrawRectangle(myPen, new Rectangle(i * 150 + 20, 10, 150, 150));
g.DrawString("Tile",myFont,sBrush,new PointF(180,170));
i += 1;
//对图片使用水平翻转排列方式
tBrush.WrapMode = WrapMode.TileFlipX;
g.FillRectangle(tBrush, new Rectangle(i * 150 + 40, 10, 150, 150));
g.DrawRectangle(myPen, new Rectangle(i * 150 + 40, 10, 150, 150));
g.DrawString("TileFlipX", myFont, sBrush, new PointF(360, 170));
//对图片使用垂直翻转排列方式
tBrush.WrapMode = WrapMode.TileFlipY;
g.FillRectangle(tBrush, new Rectangle(10, 200, 150, 150));
g.DrawRectangle(myPen, new Rectangle(10, 200, 150, 150));
g.DrawString("TileFlipX", myFont, sBrush, new PointF(10, 360));
//对图片使用水平、垂直同时翻转排列方式
tBrush.WrapMode = WrapMode.TileFlipXY;
g.FillRectangle(tBrush, new Rectangle(180, 200, 150, 150));
g.DrawRectangle(myPen, new Rectangle(180, 200, 150, 150));
g.DrawString("TileFlipXY", myFont, sBrush, new PointF(180, 360));
//释放资源
myPen.Dispose();
myFont.Dispose();
sBrush.Dispose();
tBrush.Dispose();
bmp.Dispose();
}