本文实例讲述了C#自定义签名章实现方法。分享给大家供大家参考。具体实现方法如下:
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
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; namespace WfpApp { public class DrawCachet { /// <summary> /// 自定义椭圆形签名章 /// </summary> /// <param name="Width">宽度,画出的签名章只有这宽度的一半</param> /// <param name="Height">高度,画出的签名章只有这高度的一半</param> /// <param name="test">签名章上名字</param> /// <param name="IsRotate">签名章是否旋转角度</param> /// <param name="angle">旋转角度的大小</param> /// <returns></returns> public static Bitmap GetDrawCircleCachet( int Width, int Height, string test, bool IsRotate, int angle) { //记录圆画笔的粗细 int Circle_Brush = 2; //画布 Bitmap bitmap = new Bitmap(Width, Height); //定义字符串的样式 Font var_Font = new Font( "Arial" , 13, FontStyle.Bold); //定义一个矩形 ,设置圆的绘制区 Rectangle rect = new Rectangle(10, 10, Width / 2, Height / 2); //实例化Graphic类 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //消除绘制图形的锯齿,平滑 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //以白色清空panelCachet画布背景 g.Clear(Color.White); //设置画笔的颜色 Pen mypen = new Pen(Color.Red, Circle_Brush); //绘制圆 g.DrawEllipse(mypen, rect); //设置文字的字体样式 Font star_Font = new Font( "Arial" , 12, FontStyle.Regular); //对字符串进行宽度和长度测量 SizeF var_Size = g.MeasureString(test, star_Font); float CircleZjW = rect.Width + 2 * Circle_Brush; float CircleZJH = rect.Height + 2 * Circle_Brush; float x = (CircleZjW - var_Size.Width) / 2F + rect.X; float y = (CircleZJH - var_Size.Height) / 2F + rect.Y; //在指定的位置绘制文字 g.DrawString(test, star_Font, mypen.Brush, new PointF(x, y)); if (IsRotate) bitmap = Rotate(bitmap, angle); return bitmap; } /// <summary> /// 自定义矩形签名章 /// </summary> /// <param name="width">宽度,画出的签名章只有这宽度的一半</param> /// <param name="height">高度,画出的签名章只有这高度的一半</param> /// <param name="name">签名章上名字</param> /// <param name="IsRotate">签名章是否旋转角度</param> /// <param name="angle">旋转角度的大小</param> /// <returns></returns> public static Bitmap GetDrawRectangle( int width, int height, string name, bool IsRotate, int angle) { //记录圆画笔的粗细 int Circle_Brush = 2; //画布 Bitmap bitmap = new Bitmap(width, height); //定义字符串的样式 Font var_Font = new Font( "Arial" , 13, FontStyle.Bold); //定义一个矩形 ,设置圆的绘制区 Rectangle rect = new Rectangle(10, 10, width / 2, height / 2); //实例化Graphic类 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //消除绘制图形的锯齿,平滑 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //以白色清空panelCachet画布背景 g.Clear(Color.White); //设置画笔的颜色 Pen mypen = new Pen(Color.Red, Circle_Brush); //绘制圆 g.DrawRectangle(mypen, rect); //设置文字的字体样式 Font star_Font = new Font( "Arial" , 12, FontStyle.Regular); //对字符串进行宽度和长度测量 SizeF var_Size = g.MeasureString(name, star_Font); float CircleZjW = rect.Width + 2 * Circle_Brush; float CircleZJH = rect.Height + 2 * Circle_Brush; float x = (CircleZjW - var_Size.Width) / 2F + rect.X; float y = (CircleZJH - var_Size.Height) / 2F + rect.Y; //在指定的位置绘制文字 g.DrawString(name, star_Font, mypen.Brush, new PointF(x, y)); if (IsRotate) bitmap = Rotate(bitmap, angle); return bitmap; } /// <summary> /// 签名章旋转 /// </summary> /// <param name="b">Bitmap图章</param> /// <param name="angle">旋转度</param> /// <returns></returns> static Bitmap Rotate(Bitmap b, int angle) { angle = angle % 360; //弧度转换 double radian = angle * Math.PI / 180.0; double cos = Math.Cos(radian); double sin = Math.Sin(radian); //原图的宽和高 int w = b.Width; int h = b.Height; int W = ( int )(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin))); int H = ( int )(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos))); //目标位图 Bitmap dsImage = new Bitmap(W, H); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //计算偏移量 Point Offset = new Point((W - w) / 2, (H - h) / 2); //构造图像显示区域:让图像的中心与窗口的中心点一致 Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h); Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2); g.TranslateTransform(center.X, center.Y); g.RotateTransform(360 - angle); //恢复图像在水平和垂直方向的平移 g.TranslateTransform(-center.X, -center.Y); g.DrawImage(b, rect); //重至绘图的所有变换 g.ResetTransform(); g.Save(); g.Dispose(); //dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); return dsImage; } } } |
希望本文所述对大家的C#程序设计有所帮助。