• 自动生成电子印章


           网络办公正逐渐成为常态,无纸化办公也是一个潮流,这二者需要电子签章,最简单的方法就是在纸上盖一个章然后扫描成电子图片文件,最后在你的系统加载这个签章电子图片文件。但这样就会些不理想的地方,如果不是透明的,叠加在有文字等的地方会遮盖了原来的内容;如果做成透明的,图片会失真,看上去很不真实。

           那就用代码画一个签章吧,本来以为是挺简单,其实不是。大小、形状、颜色这些都很受容易处理,难点就在文字按椭圆曲线排列上,涉及到字间距、倾斜角度等,实现起来还是要花一点时间的。

            既然是要用代码来画,那就要用到 Graphics 这个GDI了。为了画出高质量边缘无锯齿的透明图形,需要对Graphics的绘画质量进行设置,并清除背景色。

    1. Image img = new Bitmap(imgWidth, imgHeight);
    2. Graphics g = Graphics.FromImage(img);
    3. g.SmoothingMode = SmoothingMode.HighQuality;
    4. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    5. g.CompositingQuality = CompositingQuality.HighQuality;
    6. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    7. g.Clear(Color.Transparent);

           印章形状有圆形和椭圆形二种,圆形的话高和宽调成165的话打印出来和实际印章大小比较接近,椭圆形的宽和高则设置成197和131,当然在实际中是有不同大小的印章,只要调整宽和高就可。设置好宽和高后就可定义要画的图形大小和位置了,这里包含印章外边框和印章名称二个。

           印章外边框的大小和位置

           Rectangle rect = new Rectangle(new Point(2, 2), new Size(imgWidth - 5, imgHeight - 5));

            圆形印章名称的大小和位置

            Rectangle rectString = new Rectangle(new Point(6, 6), new Size(imgWidth - 12, imgHeight - 12));

           椭圆形印章名称的大小和位置

           rectString = new Rectangle(new Point(9, 9), new Size(imgWidth - 16, imgHeight - 16));

           画印章外边框比较容易,直接画一个宽度为4的椭圆开就好了,圆形当椭圆一处理

           g.DrawEllipse(new Pen(foreColor, 4), rect);

          还要确定印章中心点的坐标

          Point center = new Point((imgWidth - 1) / 2, (imgHeight - 1) / 2);

           印章名称的绘画就复杂一点,为了文字的左右对称,需要设置绘画文字的起始角度、字间距和字体。实质上是把文字文字均匀地附加在圆形路径上。

    1. public void DrawEllipseString(Rectangle rect, Graphics g, Font font, Color foreColor, float startAngle, string str, bool isFill, int split)
    2. {
    3. Point origin = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
    4. StringFormat format = new StringFormat()
    5. {
    6. Alignment = StringAlignment.Center,
    7. LineAlignment = StringAlignment.Center,
    8. };
    9. if (rect.Width == rect.Height)
    10. {
    11. try
    12. {
    13. g.TranslateTransform(origin.X, origin.Y);
    14. g.RotateTransform(startAngle);
    15. float angle = startAngle + 90;
    16. foreach (var c in str)
    17. {
    18. SizeF txtSize = g.MeasureString(c.ToString(), font);
    19. Point pointB = GetPiePoint(rect, angle);
    20. Double distance = GetRealDistance(origin, pointB);
    21. int radius = (int)(distance - txtSize.Height / 2);
    22. PointF pointF = new PointF(0, -radius);
    23. g.DrawString(c.ToString(), font, new SolidBrush(foreColor), pointF, format);
    24. float fltAngle = 360f / str.Length;
    25. if (!isFill) fltAngle = (float)((txtSize.Width + split - 2) / (rect.Width * Math.PI) * 360);
    26. g.RotateTransform(fltAngle);
    27. angle += fltAngle;
    28. }
    29. g.ResetTransform();
    30. }
    31. catch { }
    32. }
    33. else
    34. {
    35. float angle = startAngle - 90;
    36. SizeF txtSize = g.MeasureString(str.ToString(), font);
    37. rect = new Rectangle(rect.X + (int)txtSize.Height / 2, rect.Y + (int)txtSize.Height / 2, rect.Width - (int)txtSize.Height, rect.Height - (int)txtSize.Height);
    38. try
    39. {
    40. for (int i = 0; i < str.Length; i++)
    41. {
    42. txtSize = g.MeasureString(str[i].ToString(), font);
    43. Point pointB = GetPiePoint(rect, angle);
    44. double distance = GetRealDistance(origin, pointB);
    45. g.TranslateTransform(pointB.X, pointB.Y);
    46. if (angle == -90)
    47. {
    48. g.RotateTransform(angle + 90);
    49. }
    50. else if (angle == 0)
    51. {
    52. g.RotateTransform(angle + 90);
    53. }
    54. else if (angle == 90)
    55. {
    56. g.RotateTransform(angle + 90);
    57. }
    58. else if (angle == 180)
    59. {
    60. g.RotateTransform(angle + 90);
    61. }
    62. else if (angle == 270)
    63. {
    64. g.RotateTransform(angle + 90);
    65. }
    66. else if (angle == 360)
    67. {
    68. g.RotateTransform(angle - 45);
    69. }
    70. else
    71. {
    72. double a = rect.Width / 2;
    73. double b = rect.Height / 2;
    74. if (rect.Height > rect.Width)
    75. {
    76. a = rect.Height / 2;
    77. b = rect.Width / 2;
    78. }
    79. double c = Math.Sqrt(a * a - b * b);
    80. Point f1 = new Point((int)(origin.X - c), origin.Y);
    81. Point f2 = new Point((int)(origin.X + c), origin.Y);
    82. if (rect.Height > rect.Width)
    83. {
    84. f1 = new Point(origin.X, (int)(origin.Y - c));
    85. f2 = new Point(origin.X, (int)(origin.Y + c));
    86. }
    87. double pf1 = GetRealDistance(f1, pointB);
    88. double pf2 = GetRealDistance(f2, pointB);
    89. double f1f2 = GetRealDistance(f1, f2);
    90. double PC = Math.Acos((distance * distance + pf2 * pf2 - c * c) / (2 * distance * pf2)) / Math.PI * 180;
    91. if (angle > 270) PC = Math.Acos((distance * distance + pf1 * pf1 - c * c) / (2 * distance * pf1)) / Math.PI * 180;
    92. if (angle < 90) PC = Math.Acos((distance * distance + pf1 * pf1 - c * c) / (2 * distance * pf1)) / Math.PI * 180;
    93. if (PC.ToString() == "NaN") PC = 0;
    94. double P = Math.Acos((pf1 * pf1 + pf2 * pf2 - f1f2 * f1f2) / (2 * pf1 * pf2)) / Math.PI * 180;
    95. double Q = P / 2 - PC;
    96. if (P < 0) Q = 0;
    97. if (P == 0) Q = 0;
    98. if (Q.ToString() == "非数字") Q = 0;
    99. if (Q < 0) Q = 0;
    100. float angleQ = angleQ = angle + 90 + (float)Q;
    101. if (angle > 90 && angle < 180) angleQ = angle + 90 - (float)Q;
    102. if (angle > 270 && angle < 360) angleQ = angle + 90 - (float)Q;
    103. if (rect.Height > rect.Width) angleQ = angle + 90 - (float)Q;
    104. g.RotateTransform(angleQ);
    105. }
    106. g.TranslateTransform(-pointB.X, -pointB.Y);
    107. g.DrawString(str[i].ToString(), font, new SolidBrush(foreColor), pointB, format);
    108. g.ResetTransform();
    109. float fltAngle = 360f / str.Length;
    110. if (!isFill)
    111. {
    112. double stringWidth = txtSize.Width + split - 2;
    113. for (float n = angle; n < 720; n += 0.1F)
    114. {
    115. Point pointN = GetPiePoint(rect, n);
    116. double stringN = GetRealDistance(pointN, pointB);
    117. if (stringN > stringWidth)
    118. {
    119. fltAngle = n - angle;
    120. break;
    121. }
    122. }
    123. }
    124. angle += fltAngle;
    125. if (angle > 360) angle -= 360;
    126. }
    127. }
    128. catch { }
    129. }
    130. }

    这里面要计算每一个文字的起始角度和坐标,还要计算二个点之间的距离

    1. public Point GetPiePoint(Rectangle lpRect, float angle)
    2. {
    3. Point pt = new Point();
    4. double a = lpRect.Width / 2.0f;
    5. double b = lpRect.Height / 2.0f;
    6. if (a == 0 || b == 0) return new Point(lpRect.X, lpRect.Y);
    7. //弧度
    8. double radian = angle * Math.PI / 180.0f;
    9. //获取弧度正弦值
    10. double yc = Math.Sin(radian);
    11. //获取弧度余弦值
    12. double xc = Math.Cos(radian);
    13. //获取曲率 r = ab/\Sqrt((a.Sinθ)^2+(b.Cosθ)^2
    14. double radio = (a * b) / Math.Sqrt(Math.Pow(yc * a, 2.0) + Math.Pow(xc * b, 2.0));
    15. //计算坐标
    16. double ax = radio * xc;
    17. double ay = radio * yc;
    18. pt.X = (int)(lpRect.X + a + ax);
    19. pt.Y = (int)(lpRect.Y + b + ay);
    20. return pt;
    21. }
    22. public double GetRealDistance(Point pointA, Point pointB)
    23. {
    24. double distance = Math.Sqrt(Math.Pow(pointA.X - pointB.X, 2.0) + Math.Pow(pointA.Y - pointB.Y, 2.0));
    25. return distance;
    26. }

    印章中间的五角星形可以用特殊字符来做,但大小等的控制不如直接画线来得方便。

    1. int radius = 27;
    2. PointF[] pentagons = new PointF[] { new PointF(center.X, center.Y - radius),
    3. new PointF((float)(center.X + radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
    4. new PointF((float)(center.X + radius * Math.Sin(36 * Math.PI / 180)), (float)(center.Y + radius * Math.Cos(36* Math.PI / 180))),
    5. new PointF((float)(center.X - radius * Math.Sin(36 * Math.PI / 180)),(float)( center.Y + radius * Math.Cos(36 * Math.PI / 180))),
    6. new PointF((float)(center.X - radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
    7. };
    8. GraphicsPath path = new GraphicsPath(FillMode.Winding);
    9. path.AddLine(pentagons[0], pentagons[2]);
    10. path.AddLine(pentagons[2], pentagons[4]);
    11. path.AddLine(pentagons[4], pentagons[1]);
    12. path.AddLine(pentagons[1], pentagons[3]);
    13. path.AddLine(pentagons[3], pentagons[0]);
    14. path.CloseFigure();
    15. g.FillPath(new SolidBrush(foreColor), path);

           印章的中间和底部文字相对简单,把字体设置小一点直接画就是,注意区分圆形和椭圆形。

    1. if (showCenterString)
    2. {
    3. if (isEllipse)
    4. {
    5. g.DrawString(centerString, new Font(font.Name, font.Size - 1), new SolidBrush(foreColor), center, format);
    6. }
    7. else
    8. {
    9. g.DrawString(centerString, new Font(font.Name, font.Size - 4), new SolidBrush(foreColor), center, format);
    10. }
    11. }
    12. if (showBottomString)
    13. {
    14. if (isEllipse)
    15. {
    16. g.DrawString(bottomString, new Font(font.Name, font.Size - 1), new SolidBrush(foreColor), center.X, center.Y + 35, format);
    17. }
    18. else
    19. {
    20. g.DrawString(bottomString, new Font(font.Name, font.Size - 4), new SolidBrush(foreColor), center.X, center.Y + 50, format);
    21. }
    22. }

    自动生成电子印章完整实例下载

  • 相关阅读:
    MySQL数据库基本操作
    第三章 内存管理 七、具有快表的地址变换结构
    R语言数据分析案例-股票可视化分析
    Java环境配置无效
    郑州分销商城小程序开发完成后如何营销?
    【java8新特性】02:常见的函数式接口
    python机器学习 一元线性回归 梯度下降法的实现 【Python机器学习系列(四)】
    c++之类和对象
    网络安全——钓鱼邮件和网站克隆
    计算机毕设 python opencv 机器视觉图像拼接算法
  • 原文地址:https://blog.csdn.net/xgh815/article/details/126452134