C#控件picturebox实现画图功能

时间:2022-09-04 12:45:24

本文实例为大家分享了c# picturebox实现画图功能的具体代码,供大家参考,具体内容如下

在form上添加 一个picturebox,一个button控件

如图所示:

C#控件picturebox实现画图功能

这样我们的绘画面板就弄好了,把picturebox的dock属性设置为fill,按键为清屏的作用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
private point p1, p2;//定义两个点(启点,终点)
private static bool drawing=false;//设置一个启动标志
private void picturebox1_mousedown(object sender, mouseeventargs e)
    {
        
       p1 = new point(e.x, e.y);
       p2 = new point(e.x, e.y);
        drawing = true;
      
    }
 
private void picturebox1_mouseup(object sender, mouseeventargs e)
    {
      drawing = false;
    }
private void picturebox1_mousemove(object sender, mouseeventargs e)
 
    {
     
      graphics g = picturebox1.creategraphics();
      if(e.button ==mousebuttons.left)
      {
        if (drawing)
        {
          //drawing = true;
          point currentpoint = new point(e.x, e.y);
          g.smoothingmode = system.drawing.drawing2d.smoothingmode.antialias;//消除锯齿
          g.drawline(new pen(color.blue, 2), p2,currentpoint);
          
          p2.x = currentpoint.x;
          p2.y = currentpoint.y;
        }
 
      }
      
    }
//清屏操作
private void button1_click(object sender, eventargs e)
{
 graphics g = picturebox1.creategraphics();
 g.clear(color.white);
}

C#控件picturebox实现画图功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zjq2010014137/article/details/18270143