首頁 > 軟體

C#實現自動生成電子印章

2022-08-23 18:02:23

網路辦公正逐漸成為常態,無紙化辦公也是一個潮流,這二者需要電子簽章,最簡單的方法就是在紙上蓋一個章然後掃描成電子圖片檔案,最後在你的系統載入這個簽章電子圖片檔案。但這樣就會些不理想的地方,如果不是透明的,疊加在有文字等的地方會遮蓋了原來的內容;如果做成透明的,圖片會失真,看上去很不真實。

那就用程式碼畫一個簽章吧,本來以為是挺簡單,其實不是。大小、形狀、顏色這些都很受容易處理,難點就在文字按橢圓曲線排列上,涉及到字間距、傾斜角度等,實現起來還是要花一點時間的。

既然是要用程式碼來畫,那就要用到 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#電子印章內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com