In the previous piece has been introduced to you how to use GDI + to draw a simple image, this piece continues to introduce some other drawing knowledge.
1. First, let's look at the pen we used in the last piece.
Pen properties are mainly: color (color), dashcap (dash End shape), dashstyle (dotted style), endcap (line End shape), startcap (thread shape), Width (thickness), and so on.
We can use pen to draw dashes, lines with arrows, etc.
Pen p = new Pen(Color.Blue, 5);//设置笔的粗细为,颜色为蓝色
Graphics g = this.CreateGraphics();
//画虚线
p.DashStyle = DashStyle.Dot;//定义虚线的样式为点
g.DrawLine(p, 10, 10, 200, 10);
//自定义虚线
p.DashPattern = new float[] { 2, 1 };//设置短划线和空白部分的数组
g.DrawLine(p, 10, 20, 200, 20);
//画箭头,只对不封闭曲线有用
p.DashStyle = DashStyle.Solid;//恢复实线
p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
g.DrawLine(p, 10, 30, 200, 30);
g.Dispose();
p.Dispose();
The above code runs the result:
2. Next, let's take a look at the use of brush.
Role: We can use brush to fill a variety of graphic shapes, such as rectangles, ellipses, fans, polygons and closed paths, and so on, there are several different types of painting brushes:
SolidBrush: The simplest form of a brush, drawing with a solid color
HatchBrush: Similar to SolidBrush, but you can use this class to select from a large number of preset patterns to use when drawing, instead of a solid color
TextureBrush: Drawing with textures (such as images)
LinearGradientBrush: Draw with two colors that blend along the gradient
PathGradientBrush: A unique path based on the programmer definition, drawing with a complex blend color gradient