C#实现自动生成电子印章

2022-10-07,,

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

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

既然是要用代码来画,那就要用到 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);
                }
            }

到此这篇关于c#实现自动生成电子印章的文章就介绍到这了,更多相关c#电子印章内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《C#实现自动生成电子印章.doc》

下载本文的Word格式文档,以方便收藏与打印。