本文實例為大家分享了c# picturebox實現(xiàn)畫圖功能的具體代碼,供大家參考,具體內容如下
在form上添加 一個picturebox,一個button控件
如圖所示:
這樣我們的繪畫面板就弄好了,把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); } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/zjq2010014137/article/details/18270143