网络办公正逐渐成为常态,无纸化办公也是一个潮流,这二者需要电子签章,最简单的方法就是在纸上盖一个章然后扫描成电子图片文件,最后在你的系统加载这个签章电子图片文件。但这样就会些不理想的地方,如果不是透明的,叠加在有文字等的地方会遮盖了原来的内容;如果做成透明的,图片会失真,看上去很不真实。
那就用代码画一个签章吧,本来以为是挺简单,其实不是。大小、形状、颜色这些都很受容易处理,难点就在文字按椭圆曲线排列上,涉及到字间距、倾斜角度等,实现起来还是要花一点时间的。
既然是要用代码来画,那就要用到 Graphics 这个GDI了。为了画出高质量边缘无锯齿的透明图形,需要对Graphics的绘画质量进行设置,并清除背景色。
Image img = new Bitmap(imgWidth, imgHeight);
Graphics g = Graphics.FromImage(img);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
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);
印章名称的绘画就复杂一点,为了文字的左右对称,需要设置绘画文字的起始角度、字间距和字体。实质上是把文字文字均匀地附加在圆形路径上。
public void DrawEllipseString(Rectangle rect, Graphics g, Font font, Color foreColor, float startAngle, string str, bool isFill, int split)
{
Point origin = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
};
if (rect.Width == rect.Height)
{
try
{
g.TranslateTransform(origin.X, origin.Y);
g.RotateTransform(startAngle);
float angle = startAngle + 90;
foreach (var c in str)
{
SizeF txtSize = g.MeasureString(c.ToString(), font);
Point pointB = GetPiePoint(rect, angle);
Double distance = GetRealDistance(origin, pointB);
int radius = (int)(distance - txtSize.Height / 2);
PointF pointF = new PointF(0, -radius);
g.DrawString(c.ToString(), font, new SolidBrush(foreColor), pointF, format);
float fltAngle = 360f / str.Length;
if (!isFill) fltAngle = (float)((txtSize.Width + split - 2) / (rect.Width * Math.PI) * 360);
g.RotateTransform(fltAngle);
angle += fltAngle;
}
g.ResetTransform();
}
catch { }
}
else
{
float angle = startAngle - 90;
SizeF txtSize = g.MeasureString(str.ToString(), font);
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);
try
{
for (int i = 0; i < str.Length; i++)
{
txtSize = g.MeasureString(str[i].ToString(), font);
Point pointB = GetPiePoint(rect, angle);
double distance = GetRealDistance(origin, pointB);
g.TranslateTransform(pointB.X, pointB.Y);
if (angle == -90)
{
g.RotateTransform(angle + 90);
}
else if (angle == 0)
{
g.RotateTransform(angle + 90);
}
else if (angle == 90)
{
g.RotateTransform(angle + 90);
}
else if (angle == 180)
{
g.RotateTransform(angle + 90);
}
else if (angle == 270)
{
g.RotateTransform(angle + 90);
}
else if (angle == 360)
{
g.RotateTransform(angle - 45);
}
else
{
double a = rect.Width / 2;
double b = rect.Height / 2;
if (rect.Height > rect.Width)
{
a = rect.Height / 2;
b = rect.Width / 2;
}
double c = Math.Sqrt(a * a - b * b);
Point f1 = new Point((int)(origin.X - c), origin.Y);
Point f2 = new Point((int)(origin.X + c), origin.Y);
if (rect.Height > rect.Width)
{
f1 = new Point(origin.X, (int)(origin.Y - c));
f2 = new Point(origin.X, (int)(origin.Y + c));
}
double pf1 = GetRealDistance(f1, pointB);
double pf2 = GetRealDistance(f2, pointB);
double f1f2 = GetRealDistance(f1, f2);
double PC = Math.Acos((distance * distance + pf2 * pf2 - c * c) / (2 * distance * pf2)) / Math.PI * 180;
if (angle > 270) PC = Math.Acos((distance * distance + pf1 * pf1 - c * c) / (2 * distance * pf1)) / Math.PI * 180;
if (angle < 90) PC = Math.Acos((distance * distance + pf1 * pf1 - c * c) / (2 * distance * pf1)) / Math.PI * 180;
if (PC.ToString() == "NaN") PC = 0;
double P = Math.Acos((pf1 * pf1 + pf2 * pf2 - f1f2 * f1f2) / (2 * pf1 * pf2)) / Math.PI * 180;
double Q = P / 2 - PC;
if (P < 0) Q = 0;
if (P == 0) Q = 0;
if (Q.ToString() == "非数字") Q = 0;
if (Q < 0) Q = 0;
float angleQ = angleQ = angle + 90 + (float)Q;
if (angle > 90 && angle < 180) angleQ = angle + 90 - (float)Q;
if (angle > 270 && angle < 360) angleQ = angle + 90 - (float)Q;
if (rect.Height > rect.Width) angleQ = angle + 90 - (float)Q;
g.RotateTransform(angleQ);
}
g.TranslateTransform(-pointB.X, -pointB.Y);
g.DrawString(str[i].ToString(), font, new SolidBrush(foreColor), pointB, format);
g.ResetTransform();
float fltAngle = 360f / str.Length;
if (!isFill)
{
double stringWidth = txtSize.Width + split - 2;
for (float n = angle; n < 720; n += 0.1F)
{
Point pointN = GetPiePoint(rect, n);
double stringN = GetRealDistance(pointN, pointB);
if (stringN > stringWidth)
{
fltAngle = n - angle;
break;
}
}
}
angle += fltAngle;
if (angle > 360) angle -= 360;
}
}
catch { }
}
}
这里面要计算每一个文字的起始角度和坐标,还要计算二个点之间的距离
public Point GetPiePoint(Rectangle lpRect, float angle)
{
Point pt = new Point();
double a = lpRect.Width / 2.0f;
double b = lpRect.Height / 2.0f;
if (a == 0 || b == 0) return new Point(lpRect.X, lpRect.Y);
//弧度
double radian = angle * Math.PI / 180.0f;
//获取弧度正弦值
double yc = Math.Sin(radian);
//获取弧度余弦值
double xc = Math.Cos(radian);
//获取曲率 r = ab/\Sqrt((a.Sinθ)^2+(b.Cosθ)^2
double radio = (a * b) / Math.Sqrt(Math.Pow(yc * a, 2.0) + Math.Pow(xc * b, 2.0));
//计算坐标
double ax = radio * xc;
double ay = radio * yc;
pt.X = (int)(lpRect.X + a + ax);
pt.Y = (int)(lpRect.Y + b + ay);
return pt;
}
public double GetRealDistance(Point pointA, Point pointB)
{
double distance = Math.Sqrt(Math.Pow(pointA.X - pointB.X, 2.0) + Math.Pow(pointA.Y - pointB.Y, 2.0));
return distance;
}
印章中间的五角星形可以用特殊字符来做,但大小等的控制不如直接画线来得方便。
int radius = 27;
PointF[] pentagons = new PointF[] { new PointF(center.X, center.Y - radius),
new PointF((float)(center.X + radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
new PointF((float)(center.X + radius * Math.Sin(36 * Math.PI / 180)), (float)(center.Y + radius * Math.Cos(36* Math.PI / 180))),
new PointF((float)(center.X - radius * Math.Sin(36 * Math.PI / 180)),(float)( center.Y + radius * Math.Cos(36 * Math.PI / 180))),
new PointF((float)(center.X - radius * Math.Sin(72 * Math.PI / 180)), (float)(center.Y - radius * Math.Cos(72 * Math.PI / 180))),
};
GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.AddLine(pentagons[0], pentagons[2]);
path.AddLine(pentagons[2], pentagons[4]);
path.AddLine(pentagons[4], pentagons[1]);
path.AddLine(pentagons[1], pentagons[3]);
path.AddLine(pentagons[3], pentagons[0]);
path.CloseFigure();
g.FillPath(new SolidBrush(foreColor), path);
印章的中间和底部文字相对简单,把字体设置小一点直接画就是,注意区分圆形和椭圆形。
if (showCenterString)
{
if (isEllipse)
{
g.DrawString(centerString, new Font(font.Name, font.Size - 1), new SolidBrush(foreColor), center, format);
}
else
{
g.DrawString(centerString, new Font(font.Name, font.Size - 4), new SolidBrush(foreColor), center, format);
}
}
if (showBottomString)
{
if (isEllipse)
{
g.DrawString(bottomString, new Font(font.Name, font.Size - 1), new SolidBrush(foreColor), center.X, center.Y + 35, format);
}
else
{
g.DrawString(bottomString, new Font(font.Name, font.Size - 4), new SolidBrush(foreColor), center.X, center.Y + 50, format);
}
}
来源:https://blog.csdn.net/xgh815/article/details/126452134


猜你喜欢
- C# CefSharp 过滤 RequestHandler 图片1、方式一ChromiumWebBrowser 实现 IRequestHan
- 前言在 Java 中,跳转的实现方式有两种:请求转发和请求重定向,但二者是完全不同的,所以我们今天就来盘它。请求转发和请求重定向主要区别,包
- 最大堆最大堆的特点是父元素比子元素大,并且是一棵完全二叉树。data[1]开始存,data[0]空着不用。也可以把data[0]当成size
- 今天有同事用swagger2开发时,有一方法返回Map<String,List<Object>>出现无法解析错误。P
- 本文实例为大家分享了Java猜拳游戏的具体代码,供大家参考,具体内容如下先来看一下效果图: 首先我们创建一个Person类,这个类
- 上一篇说到Springboot整合Netty,自定义协议实现,本文聊一些拆包/沾包问题。拆包/沾包问题TCP是面向字节流的协议,在发送方发送
- 一、系统介绍本系统实现扑克的分发,抢地主,电脑自动出牌等功能。二、系统展示1.扑克分发2.抢地主3.出牌4.游戏胜利三、系统实现Card.j
- 基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下如何使用SpringMVC进行表单上的文件上传以及多个文件同
- 在这一系列教程的最后一篇中,我想谈谈GR的EventBus,在处理多线程异步任务时是多么简单而有效。AsyncTask, Loader和Ex
- 本文实例讲述了Android编程实现网络图片查看器和网页源码查看器。分享给大家供大家参考,具体如下:网络图片查看器清单文加入网络访问权限:&
- 定义最短路问题的定义为:下图左侧是一幅带权有向图,以顶点 0 为起点到各个顶点的最短路径形成的最短路径树如下图右侧所示:带权有向图的实现在实
- 一.并行LINQSystem.Linq名称空间中包含的类ParallelEnumerable可以分解查询的工作,使其分布在多个线程上。尽管E
- ChatGPT的基本介绍ChatGPT是一个用来进行自然语言处理任务的预训练模型。要使用ChatGPT,需要了解以下几点:理解预训练模型:预
- Intersect子句一、简介Intersect返回交集,交集是指同时出现在两个集合中的元素,和数据库中的Intersect方法实现功能一样
- Feign Client 超时时间配置不生效解决方案Feign Client 的 connectTimeout 和 readTimeout
- 本文实例为大家分享了使用的是iTextSharp添加PDF水印的具体代码,供大家参考,具体内容如下需要iTextSharp.dll, 下载地
- SpringBoot的自动装配是拆箱即用的基础,也是微服务化的前提。这次主要的议题是,来看看它是怎么样实现的,我们透过源代码来把握自动装配的
- 1、数组数组的引用传递public class TestDemo1{public static void main(String args[
- 如下所示:Ctrl+1或F2快速修复Ctrl+D快捷删除行Shift+Enter 快速切换到下一行,在本行的任何位置都可Ctrl+F11快速
- Maven工程分模块开发完成 父工程配置了 tomcat7插件,运行 run命令执行run指令时失败,报错信息如下:D:\java\JDK8