本文實(shí)例講述了C#自定義簽名章實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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">簽名章是否旋轉(zhuǎn)角度</param> /// <param name="angle">旋轉(zhuǎn)角度的大小</param> /// <returns></returns> public static Bitmap GetDrawCircleCachet( int Width, int Height, string test, bool IsRotate, int angle) { //記錄圓畫筆的粗細(xì) int Circle_Brush = 2; //畫布 Bitmap bitmap = new Bitmap(Width, Height); //定義字符串的樣式 Font var_Font = new Font( "Arial" , 13, FontStyle.Bold); //定義一個(gè)矩形 ,設(shè)置圓的繪制區(qū) Rectangle rect = new Rectangle(10, 10, Width / 2, Height / 2); //實(shí)例化Graphic類 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //消除繪制圖形的鋸齒,平滑 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //以白色清空panelCachet畫布背景 g.Clear(Color.White); //設(shè)置畫筆的顏色 Pen mypen = new Pen(Color.Red, Circle_Brush); //繪制圓 g.DrawEllipse(mypen, rect); //設(shè)置文字的字體樣式 Font star_Font = new Font( "Arial" , 12, FontStyle.Regular); //對(duì)字符串進(jìn)行寬度和長(zhǎng)度測(cè)量 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">簽名章是否旋轉(zhuǎn)角度</param> /// <param name="angle">旋轉(zhuǎn)角度的大小</param> /// <returns></returns> public static Bitmap GetDrawRectangle( int width, int height, string name, bool IsRotate, int angle) { //記錄圓畫筆的粗細(xì) int Circle_Brush = 2; //畫布 Bitmap bitmap = new Bitmap(width, height); //定義字符串的樣式 Font var_Font = new Font( "Arial" , 13, FontStyle.Bold); //定義一個(gè)矩形 ,設(shè)置圓的繪制區(qū) Rectangle rect = new Rectangle(10, 10, width / 2, height / 2); //實(shí)例化Graphic類 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //消除繪制圖形的鋸齒,平滑 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //以白色清空panelCachet畫布背景 g.Clear(Color.White); //設(shè)置畫筆的顏色 Pen mypen = new Pen(Color.Red, Circle_Brush); //繪制圓 g.DrawRectangle(mypen, rect); //設(shè)置文字的字體樣式 Font star_Font = new Font( "Arial" , 12, FontStyle.Regular); //對(duì)字符串進(jìn)行寬度和長(zhǎng)度測(cè)量 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> /// 簽名章旋轉(zhuǎn) /// </summary> /// <param name="b">Bitmap圖章</param> /// <param name="angle">旋轉(zhuǎn)度</param> /// <returns></returns> static Bitmap Rotate(Bitmap b, int angle) { angle = angle % 360; //弧度轉(zhuǎn)換 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))); //目標(biāo)位圖 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; //計(jì)算偏移量 Point Offset = new Point((W - w) / 2, (H - h) / 2); //構(gòu)造圖像顯示區(qū)域:讓圖像的中心與窗口的中心點(diǎn)一致 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); //恢復(fù)圖像在水平和垂直方向的平移 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; } } } |
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。