代码改变世界

绘图

2015-08-05 20:49  辉仔1  阅读(141)  评论(0编辑  收藏  举报

绘图

复制代码
  1      // 新建窗体 (.cs)
  2         // 重写OnPaint事件
  3 
  4         //1、清除绘画面
  5         //protected override void OnPaint(PaintEventArgs e)
  6         //{
  7         //    Graphics g = e.Graphics;//绘制图形的对象
  8         //    g.Clear(Color.White);
  9         //    g.Dispose();
 10         //}
 11 
 12         //2、绘制线条
 13         //protected override void OnPaint(PaintEventArgs e)
 14         //{
 15         //    Graphics g = e.Graphics;//绘制图形的对象
 16         //    //画一条直线
 17         //    Pen pen = new Pen(Color.Red);//钢笔绘制对象
 18         //    Point p1 = new Point(50, 50);
 19         //    Point p2 = new Point(150, 50);
 20         //    g.DrawLine(pen, p1, p2);
 21         //    //画宫格
 22         //    int y = 100;
 23         //    for (int i = 0; i < 5; i++)
 24         //    {
 25         //        g.DrawLine(new Pen(Color.Black), new Point(100, y), new Point(200, y));
 26         //        y += 20;
 27         //    }
 28         //    int x = 100;
 29         //    for (int i = 0; i < 5; i++)
 30         //    {
 31         //        g.DrawLine(new Pen(Color.Black), new Point(x, 100), new Point(x, 100 + 80));
 32         //        x += 25;
 33         //    }
 34         //    g.Dispose();
 35         //}
 36 
 37         //3、绘制图形
 38         //protected override void OnPaint(PaintEventArgs e)
 39         //{
 40         //    Graphics g = e.Graphics;//绘制图形的对象
 41         //    //画矩形
 42         //    Rectangle rect = new Rectangle(200, 10, 100, 50);
 43         //    g.DrawRectangle(new Pen(Color.Black), rect);
 44         //    //画椭圆
 45         //    g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(50, 50, 100, 50));
 46         //    //画圆
 47         //    g.DrawEllipse(new Pen(Brushes.Black), new Rectangle(200, 200, 100, 100));
 48         //    //画扇形
 49         //    g.DrawPie(new Pen(Brushes.Black), new Rectangle(10, 200, 100, 100), 0, 45);
 50         //    //画多边形
 51         //    Point[] points = new Point[] {
 52         //                        new Point(300,150),
 53         //                        new Point(400,150),
 54         //                        new Point(450,200),
 55         //                        new Point(250,200)
 56         //                     };
 57         //    g.DrawPolygon(new Pen(Color.Black), points);
 58         //    g.Dispose();
 59         //}
 60 
 61         //4、填充图形
 62         //protected override void OnPaint(PaintEventArgs e)
 63         //{
 64         //    Graphics g = e.Graphics;//绘制图形的对象
 65         //    //简单填充
 66         //    g.FillRectangle(Brushes.Yellow, new Rectangle(100, 100, 50, 20));
 67         //    //渐变填充
 68         //    Brush brush = new LinearGradientBrush(new Point(10, 10), new Point(60, 30), Color.Blue, Color.White);
 69         //    g.FillRectangle(brush, new Rectangle(10, 10, 50, 20));
 70         //    g.Dispose();
 71         //}
 72 
 73         //5、绘制路径
 74         //protected override void OnPaint(PaintEventArgs e)
 75         //{
 76         //    Graphics g = e.Graphics;//绘制图形的对象
 77         //    Point[] points = new Point[] {
 78         //                        new Point(300,150),
 79         //                        new Point(400,150),
 80         //                        new Point(450,200),
 81         //                        new Point(250,200)
 82         //                     };
 83         //    GraphicsPath path = new GraphicsPath(
 84         //                points, new byte[]{
 85         //                    (byte)PathPointType.Start,
 86         //                    (byte)PathPointType.Line,
 87         //                    (byte)PathPointType.Line,
 88         //                    (byte)PathPointType.Line
 89         //            });
 90         //    g.DrawPath(new Pen(Color.Black), path);
 91         //    g.Dispose();
 92         //}
 93 
 94         //6、绘制字符串
 95         protected override void OnPaint(PaintEventArgs e)
 96         {
 97             Graphics g = e.Graphics;//绘制图形的对象
 98             //普通绘制字符串
 99             Font font1 = new System.Drawing.Font("楷体_gb2312", 30);
100             g.DrawString("abc", font1, Brushes.Red, 100, 100);
101             //带格式绘制字符串
102             Font font2 = new System.Drawing.Font("宋体", 30);
103             RectangleF rect = new RectangleF(200, 200, 100, 50);
104             g.DrawRectangle(new Pen(Color.Black), new Rectangle(200, 200, 100, 50));
105             StringFormat sf = new StringFormat();//字符串格式对象
106             sf.LineAlignment = StringAlignment.Center;//设置垂直方向居中
107             sf.Alignment = StringAlignment.Center;//设置水平方向居中
108             g.DrawString("abc", font2, Brushes.Red, rect, sf);
109             g.Dispose();
110         }
111 
112         //7、绘制图片
113         //protected override void OnPaint(PaintEventArgs e)
114         //{
115         //    Graphics g = e.Graphics;//绘制图形的对象
116         //    //普通绘制
117         //    Image image = Image.FromFile("images/mouse.png");//获取图片对象
118         //    g.DrawImage(image, new Point(50, 50));
119         //    g.Dispose();
120         //}
121 
122         //8、纹理画笔绘制图片
123         //protected override void OnPaint(PaintEventArgs e)
124         //{
125         //    Graphics g = e.Graphics;//绘制图形的对象
126         //    Image image = Image.FromFile("images/mouse.png");//获取图片对象
127         //    Brush brush = new TextureBrush(image);//纹理画笔
128         //    g.DrawEllipse(new Pen(Color.Black), new Rectangle(10, 10, 500, 500));
129         //    g.FillEllipse(brush, new Rectangle(10, 10, 500, 500));
130         //    g.Dispose();
131         //}
132 
133         //9、缓冲画图(先把文本绘制到图像上,完成了所有的绘图操作后,把整个图像绘制到窗口)
134         //protected override void OnPaint(PaintEventArgs e)
135         //{
136         //    Image image = new Bitmap(300, 300);//创建画布对象
137         //    //将5根线条绘制在image上
138         //    Graphics g = Graphics.FromImage(image);//获取image的绘制对象
139         //    int x = 10;
140         //    for (int i = 0; i < 5; i++)
141         //    {
142         //        g.DrawLine(new Pen(Color.Black), new Point(10, 10), new Point(x, 100));
143         //        x += 20;
144         //    }
145         //    //将image绘制到窗体中
146         //    e.Graphics.DrawImage(image, new Point(100, 100));
147         //}