C#常用GDI+文字操作汇总
本文实例汇总了C#常用GDI+文字操作,包含了文字的投影、倒影、旋转等常见的效果,在进行C#应用程序开发中有不错的实用价值。分享给大家供大家参考之用。具体如下:
一、投影文字
privatevoidForm1_Paint(objectsender,PaintEventArgse) { //投影文字 Graphicsg=this.CreateGraphics(); //设置文本输出质量 g.TextRenderingHint=TextRenderingHint.ClearTypeGridFit; g.SmoothingMode=SmoothingMode.AntiAlias; FontnewFont=newFont("TimesNewRoman",48); Matrixmatrix=newMatrix(); //投射 matrix.Shear(-1.5f,0.0f); //缩放 matrix.Scale(1,0.5f); //平移 matrix.Translate(130,88); //对绘图平面实施坐标变换、、 g.Transform=matrix; SolidBrushgrayBrush=newSolidBrush(Color.Gray); SolidBrushcolorBrush=newSolidBrush(Color.BlueViolet); stringtext="MINGRISOFT"; //绘制阴影 g.DrawString(text,newFont,grayBrush,newPointF(0,30)); g.ResetTransform(); //绘制前景 g.DrawString(text,newFont,colorBrush,newPointF(0,30)); }
二、倒影文字
privatevoidForm1_Paint(objectsender,PaintEventArgse) { //倒影文字 BrushbackBrush=Brushes.Gray; BrushforeBrush=Brushes.Black; Fontfont=newFont("幼圆",Convert.ToInt16(40),FontStyle.Regular); Graphicsg=this.CreateGraphics(); stringtext="MINGRISOFT"; SizeFsize=g.MeasureString(text,font); intposX=(this.Width-Convert.ToInt16(size.Width))/2; intposY=(this.Height-Convert.ToInt16(size.Height))/2; g.TranslateTransform(posX,posY); intascent=font.FontFamily.GetCellAscent(font.Style); intspacing=font.FontFamily.GetLineSpacing(font.Style); intlineHeight=System.Convert.ToInt16(font.GetHeight(g)); intheight=lineHeight*ascent/spacing; GraphicsStatestate=g.Save(); g.ScaleTransform(1,-1.0F); g.DrawString(text,font,backBrush,0,-height); g.Restore(state); g.DrawString(text,font,foreBrush,0,-height); }
三、文字填充线条
privatevoidForm1_Paint(objectsender,PaintEventArgse) { //使用图像填充文字线条 TextureBrushbrush=newTextureBrush(Image.FromFile(Application.StartupPath+"\\花.jpg")); Graphicsg=e.Graphics; g.DrawString("MINGRISOFT",newFont("隶书",60),brush,newPointF(0,0)); }
四、旋转文字
privatevoidForm1_Paint(objectsender,PaintEventArgse) { //旋转显示文字 Graphicsg=e.Graphics; g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias; for(inti=0;i<=360;i+=10) { //平移Graphics对象到窗体中心 g.TranslateTransform(this.Width/2,this.Height/2); //设置Graphics对象的输出角度 g.RotateTransform(i); //设置文字填充颜色 Brushbrush=Brushes.DarkViolet; //旋转显示文字 g.DrawString("......MINGRISOFT",newFont("LucidaConsole",11f),brush,0,0); //恢复全局变换矩阵 g.ResetTransform(); } }
五、印版文字
privatevoidForm1_Paint(objectsender,PaintEventArgse) { //印版文字 inti=0; BrushbackBrush=Brushes.Black; BrushforeBrush=Brushes.Violet; Fontfont=newFont("TimesNewRoman",System.Convert.ToInt16(40),FontStyle.Regular); Graphicsg=this.CreateGraphics(); g.Clear(Color.White); stringtext="MINGRISOFT"; SizeFsize=g.MeasureString(text,font); SingleposX=(this.Width-Convert.ToInt16(size.Width))/2; SingleposY=(this.Height-Convert.ToInt16(size.Height))/3; while(i<Convert.ToInt16(20)) { g.DrawString(text,font,backBrush,posX-i,posY+i); i=i+1; } g.DrawString(text,font,foreBrush,posX,posY); }
相信本文所述实例对大家的C#程序设计有一定的帮助。