画板设计之 基于图片(pictureBox)的画笔实现

首先上图吧,这是个简单的画笔(签字笔,荧光笔,排笔)的实现,实现的原理很简单,在图片上面绘制,后面我将用WPF实现个完整的画板软件,基于微软的inkcanvas,功能涵盖设计一套画笔(纹理笔,荧光笔,排笔,魔法笔,蜡笔等),对象的加载(图片,PDF以及音视频),对象的基本操作(拉伸,移动,旋转)以及教育画板常用的基本操作,画板的导出(IWB.XML,PDF,IMAGE). 文章的更新将是不定期的,因为这个项目是我基于目前公司正在开发的QT版白板软件,用WPF技术重构的,所以时间上是有限的。

 

主要的代码:

 客户端代码其实就是一个PictureBox控件命名为: _drawcanvas

 后台代码:

 

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Drawing.Imaging;
using  System.Drawing.Drawing2D;
using  System.IO;
using  System.Collections.Generic;
 
namespace  drawCanvas {
     public  partial  class  Form1 : Form {
         public  Form1() {
             InitializeComponent();
 
             this ._drawcanvas.MouseDown += new  MouseEventHandler(_drawcanvas_MouseDown);
             this ._drawcanvas.MouseMove += new  MouseEventHandler(_drawcanvas_MouseMove);
             this ._drawcanvas.MouseUp += new  MouseEventHandler(_drawcanvas_MouseUp);
 
             this ._normalPen.Click += ( object  sender, EventArgs e) => this ._penType = PenType.k_pen;
             this ._hPen.Click += ( object  sender, EventArgs e) => this ._penType = PenType.k_hight_pen;
             this ._paipen.Click += ( object  sender, EventArgs e) => this ._penType = PenType.k_pai_pen;
         }
 
 
         void  _drawcanvas_MouseUp( object  sender, MouseEventArgs e) {
             this ._isPressed = false ;
             no_of_points = 0;
         }
 
         void  _drawcanvas_MouseMove( object  sender, MouseEventArgs e) {
             if  (_isPressed) {
                 if  (no_of_points > 3) {
                     pt[0] = pt[1]; pt[1] = pt[2]; pt[2] = pt[3];
                     pt[3].setxy(e.X, e.Y);
                     double  temp = Math.Sqrt(Math.Pow(pt[2].x - pt[1].x, 2F) + Math.Pow(pt[2].y - pt[1].y, 2F));
                     int  interpol = System.Convert.ToInt32(temp);
                     bsp(pt[0], pt[1], pt[2], pt[3], interpol);
                     int  i;
                     Graphics g = this ._drawcanvas.CreateGraphics();
 
                     int  x, y;
 
                     g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高,即消除锯齿
                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                     g.CompositingQuality = CompositingQuality.HighQuality;
 
                     for  (i = 0; i <= interpol - 1; i++) {
                         x = System.Convert.ToInt32(splinex[i]);
                         y = System.Convert.ToInt32(spliney[i]);
 
                         switch  (_penType) {
                             case  PenType.k_pai_pen:
                                 _penColor = Color.FromArgb(255, _penColor);
                                  g.DrawLine( new  Pen(_penColor, _penWidth), new  Point(x - 1, y), new  Point(x - 1 + _x_offset, y + _y_offset));
                                  g.DrawLine( new  Pen(_penColor, _penWidth), new  Point(x + 1, y), new  Point(x + 1 + _x_offset, y + _y_offset));
                                  g.DrawLine( new  Pen(_penColor, _penWidth), new  Point(x, y - 1), new  Point(x + _x_offset, y - 1 + _y_offset));
                                  g.DrawLine( new  Pen(_penColor, _penWidth), new  Point(x, y + 1), new  Point(x + _x_offset, y + 1 + _y_offset));
                                  break ;
                             case  PenType.k_pen:
                                  _penColor = Color.FromArgb(255, _penColor);
                                  g.DrawEllipse( new  Pen(_penColor, _penWidth), x - 1, y, _penWidth , _penWidth);
                                  g.DrawEllipse( new  Pen(_penColor, _penWidth), x + 1, y, _penWidth, _penWidth);
                                  g.DrawEllipse( new  Pen(_penColor, _penWidth), x, y - 1, _penWidth, _penWidth);
                                  g.DrawEllipse( new  Pen(_penColor, _penWidth), x, y + 1, _penWidth, _penWidth);
                                  break ;
                             case  PenType.k_hight_pen:
                                  _penColor = Color.FromArgb(80, _penColor.R, _penColor.G, _penColor.B);
                                  float  p = _penWidth + 8;
                                 g.DrawEllipse( new  Pen(_penColor, p), x - 1, y, _penWidth , _penWidth);
                                 g.DrawEllipse( new  Pen(_penColor, p), x + 1, y, _penWidth, _penWidth);
                                 g.DrawEllipse( new  Pen(_penColor, p), x, y - 1, _penWidth, _penWidth);
                                 g.DrawEllipse( new  Pen(_penColor, p), x, y + 1, _penWidth, _penWidth);
                                 break ;
                                 
                         }
                        
                     }
                 }
                 else  {
                     pt[no_of_points].setxy(e.X, e.Y);
                 }
 
                 no_of_points = no_of_points + 1;
             }
         }
 
         void  _drawcanvas_MouseDown( object  sender, MouseEventArgs e) {
             _isPressed = true ;
 
             no_of_points = 0;
             pt[no_of_points].setxy(e.X, e.Y);
             no_of_points = no_of_points + 1;
 
             //Graphics g = this._drawcanvas.CreateGraphics();
             //g.DrawLine(new Pen(_penColor, _penWidth), new Point(e.X,e.Y), new Point(e.X + _x_offset, e.Y + _y_offset));
         }
 
         private  Color _penColor = Color.Yellow;
         private  float  _penWidth = 2;
         private  bool  _isPressed = false ;
         private  int  _x_offset = 4;
         private  int  _y_offset = 20;
         private  PenType _penType = PenType.k_pen;
 
         public  double [] splinex = new  double [1001];
         public  double [] spliney = new  double [1001];
         public  point[] pt = new  point[6];
         public  int  no_of_points = 0;
         int [] a1 = new  int [12];
         int [] b1 = new  int [12];
 
         public  struct  point {
             public  int  x;
             public  int  y;
 
             public  void  setxy( int  i, int  j) {
                 x = i;
                 y = j;
             }
        
         // calculating the values using the algorithm
         public  void  bsp(point p1, point p2, point p3, point p4, int  divisions) {
             double [] a = new  double [5];
             double [] b = new  double [5];
             a[0] = (-p1.x + 3 * p2.x - 3 * p3.x + p4.x) / 6.0;
             a[1] = (3 * p1.x - 6 * p2.x + 3 * p3.x) / 6.0;
             a[2] = (-3 * p1.x + 3 * p3.x) / 6.0;
             a[3] = (p1.x + 4 * p2.x + p3.x) / 6.0;
             b[0] = (-p1.y + 3 * p2.y - 3 * p3.y + p4.y) / 6.0;
             b[1] = (3 * p1.y - 6 * p2.y + 3 * p3.y) / 6.0;
             b[2] = (-3 * p1.y + 3 * p3.y) / 6.0;
             b[3] = (p1.y + 4 * p2.y + p3.y) / 6.0;
 
             splinex[0] = a[3];
             spliney[0] = b[3];
 
             int  i;
             for  (i = 1; i <= divisions - 1; i++) {
                 float  t = System.Convert.ToSingle(i) / System.Convert.ToSingle(divisions);
                 splinex[i] = (a[2] + t * (a[1] + t * a[0])) * t + a[3];
                 spliney[i] = (b[2] + t * (b[1] + t * b[0])) * t + b[3];
 
 
             }
         }
 
         private  void  button1_Click( object  sender, EventArgs e) {
             ColorDialog dlg = new  ColorDialog();
             if  (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                 _penColor = dlg.Color;
             }
         }    
 
     }
 
     public  enum  PenType {
         k_pen = 0x1,
         k_hight_pen = 0x2,
         k_pai_pen
     }
}

  

 demo下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值