c# picturebox上画图

这篇博客介绍了如何在C#的PictureBox控件上进行图形绘制。通过设置控件属性,定义起点和终点,监听鼠标事件,实现了鼠标拖动时画线的功能。同时,还提供了清屏操作的实现,当点击按钮时清除PictureBox上的所有图形。

在Form上添加 一个pictureBox,一个button控件

如图所示:

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

 

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. private Point p1, p2;//定义两个点(启点,终点)  
  2. private static bool drawing=false;//设置一个启动标志  
  3. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)  
  4.         {  
  5.                   
  6.               p1 = new Point(e.X, e.Y);  
  7.               p2 = new Point(e.X, e.Y);  
  8.                 drawing = true;  
  9.               
  10.         }  
  11.   
  12. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)  
  13.         {  
  14.             drawing = false;  
  15.         }  
  16. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)  
  17.   
  18.         {  
  19.             
  20.             Graphics g = pictureBox1.CreateGraphics();  
  21.             if(e.Button ==MouseButtons.Left)  
  22.             {  
  23.                 if (drawing)  
  24.                 {  
  25.                     //drawing = true;  
  26.                     Point currentPoint = new Point(e.X, e.Y);  
  27.                     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿  
  28.                     g.DrawLine(new Pen(Color.Blue, 2),  p2,currentPoint);  
  29.                       
  30.                     p2.X = currentPoint.X;  
  31.                     p2.Y = currentPoint.Y;  
  32.                 }  
  33.   
  34.             }  
  35.               
  36.         }  
  37. //清屏操作  
  38. private void button1_Click(object sender, EventArgs e)  
  39.         {  
  40.             Graphics g = pictureBox1.CreateGraphics();  
  41.             g.Clear(Color.White);  
  42.         }  



 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值