GDI+画电子印章

使用GDI+画一个电子印章,初次使用,请多多指教。

以下是Form代码,大家应该都会用,项目文件就不上传了。

 

效果图

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            pictureBox1.Size = new Size(W + penWith , W + penWith);
        }
        /// <summary>
        /// 画笔宽度
        /// </summary>
        const int penWith = 4;

        const int W = 200;
        const int H = W;
        /// <summary>
        /// 半径
        /// </summary>
        const int Radius = W / 2;
        /// <summary>
        /// 直径
        /// </summary>
        const int Diameter = W;
        /// <summary>
        /// 公司名字体
        /// </summary>
        Font f = new System.Drawing.Font("宋体", 24, FontStyle.Bold);
        /// <summary>
        /// 公司号码字体
        /// </summary>
        Font nf = new System.Drawing.Font("宋体", 9, FontStyle.Bold);

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Creat();
        }


        Bitmap Creat()
        {
            //创建位图
            Bitmap bt = new Bitmap(W + penWith, H + penWith );
            //创建画板
            Graphics g = Graphics.FromImage(bt);
            g.SmoothingMode = SmoothingMode.AntiAlias;//消除绘制图形的锯齿
            g.Clear(Color.White);//以白色清空背景
            //文本反锯齿
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Pen p = new System.Drawing.Pen(Color.Red, penWith);
            Rectangle rec = new Rectangle(new System.Drawing.Point(penWith, penWith), new Size(W - penWith * 2, H - penWith * 2));
            rec = new Rectangle(penWith/2, penWith/2, W, W);
            //画外接正方形
            //g.DrawRectangle(new Pen(Color.Black, penWith), rec);
            g.DrawEllipse(new Pen(Color.Red, penWith), rec);
            string star = "★";
            Font star_Font = new Font("宋体", 50, FontStyle.Regular);//设置星号的字体样式
            SizeF star_Size = g.MeasureString(star, star_Font);//对指定字符串进行测量
            //要指定的位置绘制星号
            PointF star_xy = new PointF(W / 2 - star_Size.Width / 2, W / 2 - star_Size.Height / 2);
            g.DrawString(star, star_Font, Brushes.Red, star_xy);
            //设置坐标原点为圆心
            g.TranslateTransform(Radius, Radius);
            //画十字
            //g.DrawLine(p, 0, -Radius, 0, Radius);
            //g.DrawLine(p, -Radius, 0, Radius, 0);
            string name = "众达电子(美国)有限公司";
            int total=0;
            for (int i = 0; i < name.Length; i++)
            {
                string str = name[i].ToString();
                if (str == "(" || str == ")")//对于括号特殊处理,使文本更加紧凑
                {
                    total += 18;
                }
                else
                {
                    total += 24;
                }
            }
            total -= 34;//偏移量矫正
            g.RotateTransform(-total/2);//坐标系旋转
            for (int i = 0; i < name.Length; i++)
            {
                //此循环每次都是画字符到坐标系的正上方,每次画完后旋转相应角度
                string str = name[i].ToString();
                SizeF sf = g.MeasureString(str, f);
                g.DrawString(str, f, Brushes.Red, -sf.Width / 2, -Radius + sf.Height / 8);
                if ((str == ")") || (i < name.Length - 2 && name[i + 1].ToString() == ")")||(str == "(") || (i < name.Length - 2 && name[i + 1].ToString() == "("))//发现当前是括号或者下一个是括号,则移动角度小点
                {
                    g.RotateTransform(18);
                }
                else
                {
                    g.RotateTransform(24);
                }
            }
            g.ResetTransform();
            g.TranslateTransform(Radius, Radius);
            string num = "3105345027698";

            int numRotate = 7;
            g.RotateTransform(numRotate * num.Length / 2 - numRotate/2);

            for (int i = 0; i < num.Length; i++)
            {
                //此循环每次都是画字符到坐标系的正上方,每次画完后旋转相应角度
                string str = num[i].ToString();
                SizeF sf = g.MeasureString(str, nf);
                g.DrawString(str, nf, Brushes.Red, 0-sf.Width/2, Radius - sf.Height);
                g.RotateTransform(-numRotate);
            }
            g.Save();
            g.Dispose();
            return bt;
        }


    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

random_2011

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值