使用GDI+画一个电子印章,初次使用,请多多指教。
以下是Form代码,大家应该都会用,项目文件就不上传了。
效果图
public partial class Form1 : Form { public Form1() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); pictureBox1.Size = new Size(W + penWith , W + penWith); } /// <summary> /// 画笔宽度 /// </summary> const int penWith = 4; const int W = 200; const int H = W; /// <summary> /// 半径 /// </summary> const int Radius = W / 2; /// <summary> /// 直径 /// </summary> const int Diameter = W; /// <summary> /// 公司名字体 /// </summary> Font f = new System.Drawing.Font("宋体", 24, FontStyle.Bold); /// <summary> /// 公司号码字体 /// </summary> Font nf = new System.Drawing.Font("宋体", 9, FontStyle.Bold); private void button1_Click(object sender, EventArgs e) { pictureBox1.Image = Creat(); } Bitmap Creat() { //创建位图 Bitmap bt = new Bitmap(W + penWith, H + penWith ); //创建画板 Graphics g = Graphics.FromImage(bt); g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿 g.Clear(Color.White);//以白色清空背景 //文本反锯齿 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; g.InterpolationMode = InterpolationMode.HighQualityBicubic; Pen p = new System.Drawing.Pen(Color.Red, penWith); Rectangle rec = new Rectangle(new System.Drawing.Point(penWith, penWith), new Size(W - penWith * 2, H - penWith * 2)); rec = new Rectangle(penWith/2, penWith/2, W, W); //画外接正方形 //g.DrawRectangle(new Pen(Color.Black, penWith), rec); g.DrawEllipse(new Pen(Color.Red, penWith), rec); string star = "★"; Font star_Font = new Font("宋体", 50, FontStyle.Regular);//设置星号的字体样式 SizeF star_Size = g.MeasureString(star, star_Font);//对指定字符串进行测量 //要指定的位置绘制星号 PointF star_xy = new PointF(W / 2 - star_Size.Width / 2, W / 2 - star_Size.Height / 2); g.DrawString(star, star_Font, Brushes.Red, star_xy); //设置坐标原点为圆心 g.TranslateTransform(Radius, Radius); //画十字 //g.DrawLine(p, 0, -Radius, 0, Radius); //g.DrawLine(p, -Radius, 0, Radius, 0); string name = "众达电子(美国)有限公司"; int total=0; for (int i = 0; i < name.Length; i++) { string str = name[i].ToString(); if (str == "(" || str == ")")//对于括号特殊处理,使文本更加紧凑 { total += 18; } else { total += 24; } } total -= 34;//偏移量矫正 g.RotateTransform(-total/2);//坐标系旋转 for (int i = 0; i < name.Length; i++) { //此循环每次都是画字符到坐标系的正上方,每次画完后旋转相应角度 string str = name[i].ToString(); SizeF sf = g.MeasureString(str, f); g.DrawString(str, f, Brushes.Red, -sf.Width / 2, -Radius + sf.Height / 8); if ((str == ")") || (i < name.Length - 2 && name[i + 1].ToString() == ")")||(str == "(") || (i < name.Length - 2 && name[i + 1].ToString() == "("))//发现当前是括号或者下一个是括号,则移动角度小点 { g.RotateTransform(18); } else { g.RotateTransform(24); } } g.ResetTransform(); g.TranslateTransform(Radius, Radius); string num = "3105345027698"; int numRotate = 7; g.RotateTransform(numRotate * num.Length / 2 - numRotate/2); for (int i = 0; i < num.Length; i++) { //此循环每次都是画字符到坐标系的正上方,每次画完后旋转相应角度 string str = num[i].ToString(); SizeF sf = g.MeasureString(str, nf); g.DrawString(str, nf, Brushes.Red, 0-sf.Width/2, Radius - sf.Height); g.RotateTransform(-numRotate); } g.Save(); g.Dispose(); return bt; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-07 12:18:00