C#生成電子印章源碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;

namespace Cachet
{
    public class CreatPublicSeal
    {

        Font Var_Font = new Font("Arial", 12, FontStyle.Bold);//定義字符串的字體樣式
        //Rectangle rect = new Rectangle(10, 10, 160, 160);//實例化Rectangle類
        private static int tem_Line = 160;//記錄圓的直徑
        private static int circularity_W = 4;//設置圓畫筆的粗細
        //圓線條
        private static Rectangle rect = new Rectangle(circularity_W, circularity_W, tem_Line - circularity_W * 2, tem_Line - circularity_W * 2);//設置圓的繪製區域
        private static int _letterspace = 4;//字體間距
        private static Char_Direction _chardirect = Char_Direction.Center;
        private static int _degree = 90;
        //字體圓弧所在圓
        private static int space = 16;//比外面圓圈小
        private static Rectangle NewRect = new Rectangle(new Point(rect.X + space, rect.Y + space), new Size(rect.Width - 2 * space, rect.Height - 2 * space));
        
        /// <summary>
        /// 建立公司公共印章獲得gif圖片存儲地址
        /// </summary>
        /// <param name="company">公司名字</param>
        /// <param name="department">部門名字</param>
        /// <param name="Url">圖片保存路徑</param>
        /// <returns></returns>
        public static string CreatSeal(string company, string department, string Url)
        {
            
            string star_Str = "★";
            Bitmap bMap = new Bitmap(160, 160);//畫圖初始化
            Graphics g= Graphics.FromImage(bMap);
            //Graphics g = this.panel1.CreateGraphics();//實例化Graphics類
            g.SmoothingMode = SmoothingMode.AntiAlias;//消除繪製圖形的鋸齒
            g.Clear(Color.White);//以白色清空panel1控件的背景
            Pen myPen = new Pen(Color.Red, circularity_W);//設置畫筆的顏色
            g.DrawEllipse(myPen, rect); //繪製圓

            Font star_Font = new Font("Arial", 30, FontStyle.Regular);//設置星號的字體樣式
            SizeF star_Size = g.MeasureString(star_Str, star_Font);//對指定字符串進行測量
            //要指定的位置繪製星號
            PointF star_xy = new PointF(tem_Line / 2 - star_Size.Width / 2, tem_Line / 2 - star_Size.Height / 2);
            g.DrawString(star_Str, star_Font, myPen.Brush, star_xy);
           
            //繪製中間文字
            string var_txt = department;
            //string var_txt = "財務部";
            int var_len = var_txt.Length;
            Font Var_Font = new Font("Arial", 22 - var_len*2, FontStyle.Bold);//定義部門字體的字體樣式
            SizeF Var_Size = g.MeasureString(var_txt, Var_Font);//對指定字符串進行測量
            //要指定的位置繪製中間文字
            PointF Var_xy = new PointF(tem_Line / 2 - Var_Size.Width / 2, tem_Line / 2 + star_Size.Height / 2 - Var_Size.Height/2+5);
            g.DrawString(var_txt, Var_Font, myPen.Brush, Var_xy);

            //string text_txt = "吉林省明日科技有限公司";
            string text_txt = company + "專用";
            int text_len = text_txt.Length;//獲取字符串的長度
            Font text_Font = new Font("Arial", 25 - text_len, FontStyle.Bold);//定義公司名字的字體的樣式
            Pen myPenbush = new Pen(Color.White, circularity_W);

            float[] fCharWidth = new float[text_len];
            float fTotalWidth = ComputeStringLength(text_txt, g, fCharWidth, _letterspace, _chardirect, text_Font);
            // Compute arc's start-angle and end-angle
            double fStartAngle, fSweepAngle;
            fSweepAngle = fTotalWidth * 360 / (NewRect.Width * Math.PI);
            fStartAngle = 270 - fSweepAngle / 2;
            // Compute every character's position and angle
            //PointF[] pntChars = new PointF[text_len];
            PointF[] pntChars = new PointF[text_len];
            double[] fCharAngle = new double[text_len];
            ComputeCharPos(fCharWidth, pntChars, fCharAngle, fStartAngle);
            for (int i = 0; i < text_len; i++)
            {
                DrawRotatedText(g, text_txt[i].ToString(), (float)(fCharAngle[i] + _degree), pntChars[i], text_Font, myPenbush);
            }
            string imageName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".gif";
            bMap.Save(Url + imageName);
            return Url + imageName;
        }
        /// <summary>
        /// 計算字符串總長度和每一個字符長度
        /// </summary>
        /// <param name="sText"></param>
        /// <param name="g"></param>
        /// <param name="fCharWidth"></param>
        /// <param name="fIntervalWidth"></param>
        /// <returns></returns>
        private static float ComputeStringLength(string sText, Graphics g, float[] fCharWidth, float fIntervalWidth, Char_Direction Direction, Font text_Font)
        {
            // Init字符串格式
            StringFormat sf = new StringFormat();
            sf.Trimming = StringTrimming.None;
            sf.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap
                | StringFormatFlags.LineLimit;
            // 衡量整個字符串長度
            SizeF size = g.MeasureString(sText, text_Font, (int)text_Font.Style);
            RectangleF rect = new RectangleF(0f, 0f, size.Width, size.Height);
            // 測量每一個字符大小
            CharacterRange[] crs = new CharacterRange[sText.Length];
            for (int i = 0; i < sText.Length; i++)
                crs[i] = new CharacterRange(i, 1);
            // 復位字符串格式
            sf.FormatFlags = StringFormatFlags.NoClip;
            sf.SetMeasurableCharacterRanges(crs);
            sf.Alignment = StringAlignment.Near;
            // 獲得每個字符大小
            Region[] regs = g.MeasureCharacterRanges(sText, text_Font, rect, sf);
            // Re-compute whole string length with space interval width
            float fTotalWidth = 0f;
            for (int i = 0; i < regs.Length; i++)
            {
                if (Direction == Char_Direction.Center || Direction == Char_Direction.OutSide)
                    fCharWidth[i] = regs[i].GetBounds(g).Width;
                else
                    fCharWidth[i] = regs[i].GetBounds(g).Height;
                fTotalWidth += fCharWidth[i] + fIntervalWidth;
            }
            fTotalWidth -= fIntervalWidth;//Remove the last interval width
            return fTotalWidth;
        }

        /// <summary>
        /// 求出每一個字符的所在的點,以及相對於中心的角度
        ///1.  經過字符長度,求出字符所跨的弧度;
        ///2.  根據字符所跨的弧度,以及字符起始位置,算出字符的中心位置所對應的角度;
        ///3.  因爲相對中心的角度已知,根據三角公式很容易算出字符所在弧上的點,以下圖所示;
        ///4.  根據字符長度以及間隔距離,算出下一個字符的起始角度;
        ///5.  重複1直至整個字符串結束。
        /// </summary>
        /// <param name="CharWidth"></param>
        /// <param name="recChars"></param>
        /// <param name="CharAngle"></param>
        /// <param name="StartAngle"></param>
        private static void ComputeCharPos(float[] CharWidth, PointF[] recChars, double[] CharAngle, double StartAngle)
        {
            double fSweepAngle, fCircleLength;
            //Compute the circumference
            fCircleLength = NewRect.Width * Math.PI;

            for (int i = 0; i < CharWidth.Length; i++)
            {
                //Get char sweep angle
                fSweepAngle = CharWidth[i] * 360 / fCircleLength;

                //Set point angle
                CharAngle[i] = StartAngle + fSweepAngle / 2;

                //Get char position
                if (CharAngle[i] < 270f)
                    recChars[i] = new PointF(
                        NewRect.X + NewRect.Width / 2
                        - (float)(NewRect.Width / 2 *
                        Math.Sin(Math.Abs(CharAngle[i] - 270) * Math.PI / 180)),
                        NewRect.Y + NewRect.Width / 2
                        - (float)(NewRect.Width / 2 * Math.Cos(
                        Math.Abs(CharAngle[i] - 270) * Math.PI / 180)));
                else
                    recChars[i] = new PointF(
                        NewRect.X + NewRect.Width / 2
                        + (float)(NewRect.Width / 2 *
                        Math.Sin(Math.Abs(CharAngle[i] - 270) * Math.PI / 180)),
                        NewRect.Y + NewRect.Width / 2
                        - (float)(NewRect.Width / 2 * Math.Cos(
                        Math.Abs(CharAngle[i] - 270) * Math.PI / 180)));

                //Get total sweep angle with interval space
                fSweepAngle = (CharWidth[i] + _letterspace) * 360 / fCircleLength;
                StartAngle += fSweepAngle;

            }   
        }
        /// <summary>
        /// 繪製每一個字符
        /// </summary>
        /// <param name="g"></param>
        /// <param name="_text"></param>
        /// <param name="_angle"></param>
        /// <param name="text_Point"></param>
        /// <param name="text_Font"></param>
        /// <param name="myPen"></param>
        private static void DrawRotatedText(Graphics g, string _text, float _angle, PointF text_Point, Font text_Font, Pen myPen)
        {
            // Init format
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;

            // Create graphics path
            GraphicsPath gp = new GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            int x = (int)text_Point.X;
            int y = (int)text_Point.Y;

            // Add string
            gp.AddString(_text, text_Font.FontFamily, (int)text_Font.Style, text_Font.Size, new Point(x, y), sf);

            // Rotate string and draw it
            Matrix m = new Matrix();
            m.RotateAt(_angle, new PointF(x, y));
            g.Transform = m;
            g.DrawPath(myPen, gp);
            g.FillPath(new SolidBrush(Color.Red), gp);
        }

        public enum Char_Direction
        {
            Center = 0,
            OutSide = 1,
            ClockWise = 2,
            AntiClockWise = 3,
        }
    }
}

ide

相關文章
相關標籤/搜索