绘图GDI
准备
Graphics g1;//封装到g1中
Graphics g = this.CreateGraphics();
Pen pen;//画笔(线)
Font font = new Font("宋体", 10, FontStyle.Bold);//画文字,10磅
Brush brushred = new SolidBrush(Color.Red);//实心(SolidBrush)矩形框(填充)
Rectangle rect1=new Rectangle(20,120,10,160);//起始位置,宽高
画
g4.DrawRectangle(pen, Stack4CurrentX, Stack4CurrentY, dx, dy);//画矩形框,笔,起始点,长宽
g4.DrawString(CurrentPage, font, brush, Stack4CurrentX + 4, Stack4CurrentY + 4);//画字符串,字体,填充,起始位置
- 左上角坐标原点,左手系
- color:封装了颜色对象:
三种办法:FromArgb:通过三原色构建color对象
FromKnownColor:通过已知颜色构建color对象
FromName:通过颜色名称构建(常用)
Color t1=Color.Black;
Color t2=Color.FromArgb(0,0,0);//红绿蓝,000黑
Color t3=Color.FromName("Black");
-
size
尺寸,可加减 -
point
点
pointF(浮点型) -
Rectangle
Rectangle(整数)
RectangleleF(浮点数)
绘图深入应用函数图
以点绘图,用点表示函数
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
PointF[] cur1 = new PointF[150];
for(int i=0;i<cur1.Length;i++)
{
double x = (double)i / 5;
double y = Math.Sin(x) * Math.Cos(3 * x);
cur1[i] = new PointF((float)i, (float)(y * 10 + 100));
}
g.DrawLines(Pens.Blue, cur1);
}
- Transfrom 坐标变换,本质是矩阵
给你一个坐标,乘上这个矩阵,得到心得坐标 - 通过三个变换得到
g.ScaleTransform(0.8F, 0.8F);//比例变换
g.TranslateTransform(x / 2, y / 2 + 20);//平移变换
g.RotateTransform(10);//旋转变换
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.White, 0, 0, Width, Height);
float x = g.VisibleClipBounds.Width;
float y = g.VisibleClipBounds.Height;
PointF[] pts = { new PointF(0, 0), new PointF(x / 2, 0), new PointF(x / 2, y / 2), new PointF(0, y / 2) };
Pen pen = new Pen(Color.Blue, 1.0F);
g.ScaleTransform(0.8F, 0.8F);
g.TranslateTransform(x / 2, y / 2 + 20);
for (int i = 0; i < 36; i++)
{
g.DrawBeziers(pen, pts);
g.DrawRectangle(pen, -x / 12, -y / 12, x / 6, y / 6);
g.DrawEllipse(pen, -x / 4, -y / 3, x / 2, y * 2 / 3);
g.RotateTransform(10);
}
}
PATH
渐变填充
private void button3_Click(object sender, EventArgs se)
{
GraphicsPath gp = new GraphicsPath(FillMode.Winding);
gp.AddString(//字的轮廓作为路径
"字体轮廓",//文字
new FontFamily("方正舒体"),
(int)FontStyle.Regular,
180,
new PointF(10, 20),//位置
new StringFormat());
//一个brush,这个brush是一个线性渐变的刷子
Brush brush = new LinearGradientBrush(
new PointF(0, 0), new PointF(Width, Height),
Color.Blue, Color.Red);
Graphics g = this.CreateGraphics();
g.DrawPath(Pens.Black, gp);
g.FillPath(brush, gp);
}