怎么用C#制作印章.docx

上传人:b****8 文档编号:10061324 上传时间:2023-05-23 格式:DOCX 页数:25 大小:129.57KB
下载 相关 举报
怎么用C#制作印章.docx_第1页
第1页 / 共25页
怎么用C#制作印章.docx_第2页
第2页 / 共25页
怎么用C#制作印章.docx_第3页
第3页 / 共25页
怎么用C#制作印章.docx_第4页
第4页 / 共25页
怎么用C#制作印章.docx_第5页
第5页 / 共25页
怎么用C#制作印章.docx_第6页
第6页 / 共25页
怎么用C#制作印章.docx_第7页
第7页 / 共25页
怎么用C#制作印章.docx_第8页
第8页 / 共25页
怎么用C#制作印章.docx_第9页
第9页 / 共25页
怎么用C#制作印章.docx_第10页
第10页 / 共25页
怎么用C#制作印章.docx_第11页
第11页 / 共25页
怎么用C#制作印章.docx_第12页
第12页 / 共25页
怎么用C#制作印章.docx_第13页
第13页 / 共25页
怎么用C#制作印章.docx_第14页
第14页 / 共25页
怎么用C#制作印章.docx_第15页
第15页 / 共25页
怎么用C#制作印章.docx_第16页
第16页 / 共25页
怎么用C#制作印章.docx_第17页
第17页 / 共25页
怎么用C#制作印章.docx_第18页
第18页 / 共25页
怎么用C#制作印章.docx_第19页
第19页 / 共25页
怎么用C#制作印章.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

怎么用C#制作印章.docx

《怎么用C#制作印章.docx》由会员分享,可在线阅读,更多相关《怎么用C#制作印章.docx(25页珍藏版)》请在冰点文库上搜索。

怎么用C#制作印章.docx

怎么用C#制作印章

制作印章来说,主要是如何让字均匀的显示在弧线段上,那么一般的印章要么以圆或者椭圆为底图,不过这两者的算法大致相同,为了方便说明,如下就用相对简单的圆来举例说明,如果需要做椭圆的话,可以在我的基础上进行扩展,因为核心算法是一样的,相对于圆来说,椭圆求弧长以及各个字符的位置,这两点相对麻烦些,但是这两者都可找到相应的数学公式。

 

这里首先提一点,我这篇文章部分借鉴了codeproject的一个例子,原文可以参看如下地址。

vb/net/Text_on_Path_with_vbNET.asp">

(说实话,这篇文章写得有些乱,而且对于buffer的操作近乎于疯狂)

 

由于印章的实现相对于这篇文章来说,相对简单多了,而且规律性很强,因此我自己考虑重新组织算法进行实现。

 

那么实现一个印章,大致步骤如下。

1.计算字符串总长度,以及各个字符的长度;

2.计算出字符串的起始角度;

3.求出每个字符的所在的点,以及相对于中心的角度;

4.绘制每个字符。

 

计算字符串总长度,以及各个字符的长度

这里需要用到“Graphics.MeasureString”和“Graphics.MeasureCharacterRanges”这两个方法,由于前者算出来的总长度有问题,所以需要后面进行重新计算(此外,这里我还考虑了字符最后显示方向)。

这部分的代码如下:

   ///

   ///Computestringtotallengthandeverycharlength

   ///

   ///

   ///

   ///

   ///

   ///

   privatefloatComputeStringLength(stringsText,Graphicsg,float[]fCharWidth,

       floatfIntervalWidth,

       Char_DirectionDirection)

   {

       //Initstringformat

       StringFormatsf=newStringFormat();

       sf.Trimming=StringTrimming.None;

       sf.FormatFlags=StringFormatFlags.NoClip|StringFormatFlags.NoWrap

           |StringFormatFlags.LineLimit;

       

       //Measurewholestringlength

       SizeFsize=g.MeasureString(sText,_font,(int)_font.Style);

       RectangleFrect=newRectangleF(0f,0f,size.Width,size.Height);

 

       //Measureeverycharactersize

       CharacterRange[]crs=newCharacterRange[sText.Length];

       for(inti=0;i

           crs[i]=newCharacterRange(i,1);

 

       //Resetstringformat

       sf.FormatFlags=StringFormatFlags.NoClip;

       sf.SetMeasurableCharacterRanges(crs);

       sf.Alignment=StringAlignment.Near;

 

       //Geteverycharactersize

       Region[]regs=g.MeasureCharacterRanges(sText,

           _font,rect,sf);

 

       //Re-computewholestringlengthwithspaceintervalwidth

       floatfTotalWidth=0f;

       for(inti=0;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;//Removethelastintervalwidth

 

       returnfTotalWidth;

 

   }

 

计算出字符串的起始角度

为了更好地开展文章,那么首先说说在我这篇文章中,坐标的度数位置。

详情参看如下图示。

 

对于图形角度分布有个概念后,那么对于整个字符串所跨的弧度以及起始弧度的计算,就相对比较简单了。

具体如下:

   //Computearc'sstart-angleandend-angle

   doublefStartAngle,fSweepAngle;

   fSweepAngle=fTotalWidth*360/(_rectcircle.Width*Math.PI);

   fStartAngle=270-fSweepAngle/2;

求出每个字符的所在的点,以及相对于中心的角度

这一部分相对要麻烦些,大致步骤如下。

1. 通过字符长度,求出字符所跨的弧度;

2. 根据字符所跨的弧度,以及字符起始位置,算出字符的中心位置所对应的角度;

3. 由于相对中心的角度已知,根据三角公式很容易算出字符所在弧上的点,如下图所示;

4. 根据字符长度以及间隔距离,算出下一个字符的起始角度;

5. 重复1直至整个字符串结束。

那么这部分的具体代码如下。

   ///

   ///Computeeverycharposition

   ///

   ///

   ///

   ///

   ///

   privatevoidComputeCharPos(

       float[]CharWidth,

       PointF[]recChars,

       double[]CharAngle,

       doubleStartAngle)

   {

       doublefSweepAngle,fCircleLength;

       //Computethecircumference

       fCircleLength=_rectcircle.Width*Math.PI;

       

       for(inti=0;i

       {

           //Getcharsweepangle

           fSweepAngle=CharWidth[i]*360/fCircleLength;

 

           //Setpointangle

           CharAngle[i]=StartAngle+fSweepAngle/2;

 

           //Getcharposition

           if(CharAngle[i]<270f)

               recChars[i]=newPointF(

                   _rectcircle.X+_rectcircle.Width/2

                   -(float)(_rectcircle.Width/2*

                   Math.Sin(Math.Abs(CharAngle[i]-270)*Math.PI/180)),

                   _rectcircle.Y+_rectcircle.Width/2

                   -(float)(_rectcircle.Width/2*Math.Cos(

                   Math.Abs(CharAngle[i]-270)*Math.PI/180)));

           else

               recChars[i]=newPointF(

                   _rectcircle.X+_rectcircle.Width/2

                   +(float)(_rectcircle.Width/2*

                   Math.Sin(Math.Abs(CharAngle[i]-270)*Math.PI/180)),

                   _rectcircle.Y+_rectcircle.Width/2

                   -(float)(_rectcircle.Width/2*Math.Cos(

                   Math.Abs(CharAngle[i]-270)*Math.PI/180)));

 

           //Gettotalsweepanglewithintervalspace

           fSweepAngle=(CharWidth[i]+_letterspace)*360/fCircleLength;

           StartAngle+=fSweepAngle;

 

       }

   }

绘制每个字符

由于每个字符所在的点以及相对于圆心的角度都已经计算出来,那么进行绘制就相对简单多了,这里只是通过Matrix来转换一下坐标而已。

具体代码如下。

   ///

   ///Draweveryrotatedcharacter

   ///

   ///

   ///

   ///

   ///

   privatevoidDrawRotatedText(Graphicsg,string_text,float_angle,PointF_PointCenter)

   {

       //Initformat

       StringFormatsf=newStringFormat();

       sf.Alignment=StringAlignment.Center;

       sf.LineAlignment=StringAlignment.Center;

 

       //Creategraphicspath

       GraphicsPathgp=newGraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);

       intx=(int)_PointCenter.X;

       inty=(int)_PointCenter.Y;

 

       //Addstring

       gp.AddString(_text,_font.FontFamily,(int)_font.Style,

           _font.Size,newPoint(x,y),sf);

 

       //Rotatestringanddrawit

       Matrixm=newMatrix();

       m.RotateAt(_angle,newPointF(x,y));

       g.Transform=m;

       g.DrawPath(newPen(_color),gp);

       g.FillPath(newSolidBrush(_fillcolor),gp);

   }

 

以上就是绘制印章的核心算法。

对于这个类的调用,如下即可。

   TextOnSeal_top=newTextOnSeal();

   _top.TextFont=newFont("宋体",16,FontStyle.Regular);

   _top.FillColor=Color.Red;

   _top.ColorTOP=Color.Black;

   _top.Text="中华人民共和国";

   _top.BaseString="愚翁专用章";

   _top.ShowPath=true;

   _top.LetterSpace=20;

   _top.SealSize=180;

   _top.CharDirection=Char_Direction.Center;

   _top.SetIndent(20);

 

   Graphicsg=this.CreateGraphics();

   g.DrawImage(_top.TextOnPathBitmap(),0,0);

 

   _top.CharDirection=Char_Direction.ClockWise;

   g.DrawImage(_top.TextOnPathBitmap(),180,0);

 

   _top.CharDirection=Char_Direction.AntiClockWise;

   g.DrawImage(_top.TextOnPathBitmap(),0,180);

 

   _top.SetIndent(20);

   _top.CharDirection=Char_Direction.OutSide;

   g.DrawImage(_top.TextOnPathBitmap(),180,180);

 

通过如上的代码,可以得到如下的效果。

 

其实如果做印章来说,还有很多地方需要细化,那么如果网友对此有兴趣,可以在我的基础上进行扩展,在此我就不一一述说。

 

如下是整个类的完整代码。

//---------------------------TextOnSealclass---------------------------------------

//------------------------------------------------------------------------------------

//---File:

         TextOnSeal

//---Description:

  Theclassfiletocreatesealbitmapwithtext

//---Author:

       Knight

//---Date:

         Nov.3,2006

//------------------------------------------------------------------------------------

//---------------------------{TextOnSealclass}---------------------------------------

 

namespaceSeal

{

   usingSystem;

   usingSystem.Drawing;

   usingSystem.Drawing.Drawing2D;

   usingSystem.Diagnostics;

 

   ///

   ///SummarydescriptionforTextOnSeal.

   ///

   publicclassTextOnSeal

   {

       privatestring_text;

       privateFont_font;

       privateColor_pathcolor=Color.Red;

       privateColor_color=Color.Black;

       privateColor_fillcolor=Color.Black;

       privateint_letterspace=10;

       privatebool_showpath=true;

       privateRectangle_rectcircle;

       privateRectangle_rect;

       privateint_intentlength=10;

       privateChar_Direction_chardirect=Char_Direction.Center;

       privateint_degree=90;

       privatestring_basestring;

       #regionClass_Properties

       publicChar_DirectionCharDirection

       {

           get{return_chardirect;}

           set{

               if(_chardirect!

=value)

               {

                   _chardirect=value;

                   switch(_chardirect)

                   {

                       caseChar_Direction.Center:

                           _degree=90;

                           break;

                       caseChar_Direction.ClockWise:

                           _degree=0;

                           break;

                       caseChar_Direction.OutSide:

                           _degree=-90;

                           break;

                       caseChar_Direction.AntiClockWise:

                           _degree=180;

                           break;

                   }

               }

           }

       }

 

       publicstringBaseString

       {

           get{return_basestring;}

           set{_basestring=value;}

       }

 

       publicstringText

       {

           get{return_text;}

           set{_text=value;}

       }

 

       publicFontTextFont

       {

           get{return_font;}

           set{_font=value;}

       }

 

       publicCo

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2