您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C#-Graphics-画图生成椭圆、圆形印章代码
C#Graphics画图生成椭圆、圆形印章///summary///印章属性////summarypublicclassEntityAttr{publicstringtxt;///summary///0:圆;1:椭圆////summarypublicintdrawType=0;publicintRotate=100;publicintwidth=0;publicintheight=0;///summary///名称大小////summarypublicintnameSize=18;publicstringnameFont=宋体;///summary///类型大小////summarypublicinttypeSize=18;publicstringtypeName=string.Empty;publicstringtypeFont=宋体;///summary///五角星大小////summarypublicintstarSize=40;publicstringstar=★;publicstringstarFont=宋体;///summary///记录圆画笔的粗细////summarypublicintcircle_Brush=4;publicstringsaveUrl=string.Empty;}publicpartialclassWebForm3:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){EntityAttrentityAttr=newEntityAttr();entityAttr.txt=某某科技有限公司;entityAttr.typeName=合同公用章;entityAttr.drawType=0;entityAttr.width=120;entityAttr.height=120;entityAttr.Rotate=100;entityAttr.saveUrl=/1.gif;DrawSeal(entityAttr);entityAttr=newEntityAttr();entityAttr.txt=某某科技有限公司;entityAttr.typeName=发票专用章;entityAttr.drawType=1;entityAttr.star=44012231256061132;entityAttr.starSize=18;entityAttr.width=180;entityAttr.height=120;entityAttr.Rotate=80;entityAttr.saveUrl=/2.gif;DrawSeal(entityAttr);}#region获取椭圆坐标///summary///获取椭圆坐标///x2⒈/a2+y2/b2=1///y=x*tanα////summary///paramname=R长半轴/param///paramname=r短半轴/param///paramname=Angle角度/param///returns/returnspublicstaticPointFGetEllipseXY(floatR,floatr,floatAngle){floatx=0,y=0,tan=0,Rad=0;if(Math.Abs(Angle)90)Rad=(Math.Abs(Angle)-90);elseRad=(90-Math.Abs(Angle));Rad=Rad*2*(float)Math.PI/360;tan=(float)Math.Tan(Rad);x=(float)Math.Sqrt((double)1/((double)1/(R*R)+(tan*tan)/(r*r)));y=x*tan;if(Angle0)x=0-x;if(Angle-90&&Angle90)y=0-y;x=R+x;y=r+y;returnnewPointF(x,y);}#endregionpublicvoidDrawSeal(EntityAttrentityAttr){floatbeginRotate=-entityAttr.Rotate;floatendRotate=entityAttr.Rotate;floatecllpse_width=entityAttr.width;floatecllpse_height=entityAttr.height;floatcircle_Brush=entityAttr.circle_Brush;Bitmapmap=newBitmap((int)ecllpse_width*2,(int)ecllpse_height*2);using(Graphicsg=Graphics.FromImage(map)){g.SmoothingMode=SmoothingMode.AntiAlias;//消除绘制图形的锯齿,平滑g.FillRectangle(newSolidBrush(Color.Transparent),newRectangle(0,0,(int)ecllpse_width*2,(int)ecllpse_height*2));g.DrawEllipse(newPen(newSolidBrush(Color.Red),circle_Brush),newRectangleF((2*ecllpse_width/10)-10,(2*ecllpse_height/10)-10,(8*ecllpse_width/10)*2+20,(8*ecllpse_height/10)*2+20));Fonttxt_Font=newFont(entityAttr.nameFont,entityAttr.nameSize,FontStyle.Bold);Fonttype_Font=newFont(entityAttr.typeFont,entityAttr.typeSize,FontStyle.Bold);Fontstar_Font=newFont(entityAttr.starFont,entityAttr.starSize,FontStyle.Regular);//设置星号的字体样式Penmypen=newPen(Color.Red,circle_Brush);//设置画笔的颜色//设置笔的属性,避免出现“毛刺”mypen.StartCap=LineCap.Round;mypen.EndCap=LineCap.Round;mypen.LineJoin=LineJoin.Round;g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;SizeFvar_Size=newSizeF(ecllpse_width*2,ecllpse_height*2);//实例化一个SizeFintintTemp=0;if(entityAttr.drawType==1)intTemp=10;var_Size=g.MeasureString(entityAttr.star,star_Font);//对中间字符串进行宽度和长度测量g.DrawString(entityAttr.star,star_Font,mypen.Brush,newPointF(ecllpse_width-var_Size.Width/2F,ecllpse_height-var_Size.Height/2F+intTemp));//在指定的位置绘制星号var_Size=g.MeasureString(entityAttr.typeName,type_Font);//对下面字符串进行宽度和长度测量g.DrawString(entityAttr.typeName,type_Font,mypen.Brush,newPointF(ecllpse_width-var_Size.Width/2F,ecllpse_height+var_Size.Height*2-10));//在指定的位置绘制星号char[]texts=entityAttr.txt.ToArray();floatRotate=(float)(endRotate-beginRotate)/(texts.Length-1);for(inti=0;itexts.Length;i++){MatrixmtxSave=g.Transform;floatnow_rotate=beginRotate+(Rotate*i);PointFp=GetEllipseXY(13*ecllpse_width/20,13*ecllpse_height/20,beginRotate+(Rotate*i));MatrixmtxRotate=g.Transform;p.X+=(ecllpse_width-circle_Brush*4)/4;p.Y+=(ecllpse_height-circle_Brush*4)/4;mtxRotate.RotateAt(beginRotate+(Rotate*i),newPointF(p.X+entityAttr.nameSize,p.Y+entityAttr.nameSize));g.Transform=mtxRotate;g.DrawString(texts[i].ToString(),txt_Font,newSolidBrush(Color.Red),p);g.Transform=mtxSave;}map.Save(Server.MapPath(entityAttr.saveUrl));g.Dispose();map.Dispose();}}}
本文标题:C#-Graphics-画图生成椭圆、圆形印章代码
链接地址:https://www.777doc.com/doc-4674107 .html