个性印章及公章的画法及实现

123 篇文章 2 订阅
19 篇文章 0 订阅

声明:本程序系本人原创,引用时请保留以下信息:
--------------------------------------------------------------------------------
阿山Net [MSN:a3news(AT)hotmail.com]
http://www.zpxp.com 萝卜鼠在线图形图像处理
--------------------------------------------------------------------------------

先看效果:
1.公章
公章效果演示
 2. 个人印章效果:
个性化私人印章效果演示

这里粘出关键代码:

  private void DrawSeal()
  {
   RetrieveAll();
   if(this.SealType == SealType.OfficeSeal)
   {
    DrawOfficeSeal();
   }
   else
   {
    DrawPersonalSeal();
   }

   panelDisplayResultBitmap.AutoScroll = true;
   panelDisplayResultBitmap.AutoScrollMinSize = new Size ((int)(m_Bitmap.Width * Zoom), (int)(m_Bitmap.Height * Zoom));
   panelDisplayResultBitmap.Invalidate();
  }

//得到相关参数:
  private void RetrieveAll()
  {
   int diameter = 220;
   try
   {
    diameter = int.Parse(this.txtBoxDiameter.Text);
   }
   catch(Exception exc)
   {
    MessageBox.Show("输入错误的公章直径!/n请确保是整数");
   }
   if(diameter>1000 || diameter < 100 )
   {
    MessageBox.Show("公章直径输入太大或太小了!");
    diameter = 220;
   }
   this.OfficeSealDiameter = diameter;

   string fontName = this.FontlistBox.Text;
   if(fontName == string.Empty)
   {
    fontName = "Times New Roman";
   }
   this.SelectedFontName = fontName;
   this.SelectedFontColorFromArgb = Color.FromArgb((int)this.FontColor_A.Value,(int)this.FontColor_R.Value,
    (int)this.FontColor_G.Value,(int)this.FontColor_B.Value);
  }

// 画公章:

  private void DrawOfficeSeal()
  { 
   try
   {
    OfficeSealMaker osm = new OfficeSealMaker();
    osm.FamilyName = this.SelectedFontName;
    osm.IsBold = false;
    osm.IsEmbedNotice = false; //是否保留“勿作他用”的提示信息
    osm.Diameter = this.OfficeSealDiameter;
    osm.Style = Style.PentagonalStars;//设置中间LOGO
    osm.OfficeName = this.txtBoxCompanyName.Text;
    osm.IsDrawInnerRect = false;
    osm.WordsColor = this.SelectedFontColorFromArgb;
    osm.BorderColor = this.SelectedFontColorFromArgb;
    //osm.TextVerHeight = (float)this.numericUpDownFontHeight.Value;
    osm.RatioX = (float)numericUpDownFontRatioX.Value / 100f;
    m_Bitmap = osm.Draw();
   }
   catch(Exception exc)
   {
    MessageBox.Show(exc.Message,"发生错误");
   }
  }

//画私章:

  private void DrawPersonalSeal()
  {
   //int stampWidth = int.Parse(this.txtBoxDiameter.Text);
   PersonalSeal personalSeal = new PersonalSeal();
   personalSeal.SealColor = this.SelectedFontColorFromArgb;
   bool isYinSeal = false;
   
   if(this.cmbBoxYinYang.SelectedItem != null && this.cmbBoxYinYang.SelectedItem.ToString() == "阴章")
   {
    isYinSeal = true;
   }
   personalSeal.IsYinSeal = isYinSeal;
   string sealWords = this.txtBoxPersonalSealWords.Text.Trim();
   if(sealWords.Length != 4)
   {
    sealWords = "美中印章";
   }
   personalSeal.SealWords = sealWords;
   personalSeal.FontFamilyName = this.SelectedFontName;
   personalSeal.IsDrawInnerBorder = false;
   personalSeal.BorderWidth = 10;
   personalSeal.Celling = 0;

   int personalSealWidth = 100;
   try
   {
    personalSealWidth = int.Parse(this.txtBoxWidth.Text);
   }
   catch(Exception exc)
   {
    MessageBox.Show("输入有误,请输入整数!");
   }

   int personalSealHeight = 100;
   try
   {
    personalSealHeight = int.Parse(this.txtBoxHeight.Text);
   }
   catch(Exception exc)
   {
    MessageBox.Show("输入有误,请输入整数!");
   }

   personalSeal.Width = personalSealWidth;
   personalSeal.Height = personalSealHeight;

   m_Bitmap = (Bitmap)personalSeal.Draw();
  }

//私人印章的画法:
public System.Drawing.Image Draw()
  {
   Bitmap m_Bitmap;
   Color sealColor = Color.Red;
   int sealWidth = this.Width;
   int sealHeight = this.Height;
   int celling = this.Celling;
   int penWidth = this.BorderWidth;
   int cornerRadius = this.CornerRadius;
   Rectangle rect = new Rectangle(0, 0, sealWidth, sealHeight);
   Rectangle rectInner = new Rectangle(penWidth + celling, penWidth + celling, sealWidth - (penWidth + celling) * 2, sealHeight - (penWidth + celling) * 2);
   m_Bitmap = new Bitmap(sealWidth, sealHeight);
   Graphics g = Graphics.FromImage(m_Bitmap);
   g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   //g.Clear(Color.White);

   Pen pen = new Pen(sealColor, penWidth);
   pen.Alignment = PenAlignment.Inset;

   //画个人印章的外部圆角矩形
   RoundRectangeHelper.DrawRoundRectangle(g, pen, rect, cornerRadius);

   pen = new Pen(this.SealColor, this.InnerBorderWidth);
   if(this.IsDrawInnerBorder)
   {
    g.DrawRectangle(pen, rectInner);
   }

   //画字
   Font font;
   try
   {
    font = new Font(this.FontFamilyName, 20f);
   }
   catch(Exception exc)
   {
    font = new Font("Times New Roman",20f);
   }
   Color fillColor;
   if(this.IsYinSeal)
   {
    fillColor = Color.White;
    g.FillRectangle(new SolidBrush(this.SealColor), rectInner);
   }
   else
   {
    fillColor = this.SealColor;
   }

   string word = this.SealWords;
   int[] orderSrc = {1,2,3,4};
   int[] orderDest = {2,4,1,3};
   char[] words = word.ToCharArray();
   char[] wordsTmp = new char[words.Length];
   words.CopyTo(wordsTmp, 0);
   for(int i = 0; i < orderSrc.Length; i++)
   {
    words[orderDest[i] - 1] = wordsTmp[orderSrc[i] - 1];
   }

   Rectangle rect1 = new Rectangle(rectInner.X, rectInner.Y, rectInner.Width/2, rectInner.Height/2);
   Rectangle rect2 = new Rectangle(rectInner.X + rectInner.Width/2, rectInner.Y, rectInner.Width/2, rectInner.Height/2);
   Rectangle rect3 = new Rectangle(rectInner.X, rectInner.Y + rectInner.Height/2, rectInner.Width/2, rectInner.Height/2);
   Rectangle rect4 = new Rectangle(rectInner.X + rectInner.Width/2, rectInner.Y + rectInner.Height/2, rectInner.Width/2, rectInner.Height/2);
   Rectangle[] rects = new Rectangle[]
    {
     rect1, rect2, rect3, rect4
    };

   for(int i = 0; i < 4; i++)
   {
    DrawWordIntoRectangle(g, words[i].ToString(), font, fillColor, rects[i]);
   }

   pen.Dispose();
   g.Dispose();

   return m_Bitmap;
  }

(只贴了关键代码, 全部源码将放在我正在写的《来点创意嘛!.Net图形图像快乐程序设计
——GDI+之Windows应用程序和Web程序实战》(暂定名)一书中.)

本文的后续WPF版本:
WPF正圆形公章制作之2
  (http://blog.csdn.net/johnsuna/archive/2008/04/28/2336495.aspx

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值