使用GDI+绘制高质量图和字体
对于GDI+,在正常的操作,Bitmap-- Graphcis -- DrawImage或者DrawString ,生成图片的话,会产生很多杂点,或者是图片质量不稳定..尤其是在读取图片后,生成缩略图之后,文件会被压缩而失真..
主要原因是因为没有重新设置Graphics的几个属性..
1.Graphics.SmoothingMode属性: 例如SmoothingMode.HighQuality可以产生高质量图片,但是效率低.
2.Graphics.CompositingQuality 属性: 例如:CompositingQuality.HighQuality也是产生高质量图,效率低下.
3.Graphics.InterpolationMode 属性,例如:InterpolationMode.HighQualityBicubic与前两个也是同样的效果.
这三个属性的值都是enum,具体的enum参数可以查看MSDN的说明..在这里就我不赘述..
如果是对图片进行放大,缩小,可以调整Graphics.CompositingQuality 和Graphics.InterpolationMode 两个属性..如果是图片生成,则可以调整Graphics.SmoothingMode属性..
另外一个问题就是关于文字生成的..按照正常的模式生成的文字,可以很明显的看到文字带有锯齿..解决的办法也是需要修改Graphics的一个属性: Graphics.TextRenderingHint...注意一点,修改TextRenderingHint的话,需要引入System.Drawing.Text,例如:Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
经过对这四个属性的修改,操作大部分的图片之后,产生的结果都是比较让人满意的..
在这里提供一个简单的例子..是生成印章签名的效果..提供四种字体选择..并产生一个图片..如果你的机器中没有安装指定的几种字体,你需要把字体修改一下...直接把代码拷贝..就可以看到效果...
ASPX页面:
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3<HTML>
4 <HEAD>
5 <title>underWrite</title>
6 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
7 <meta name="CODE_LANGUAGE" Content="C#">
8 <meta name="vs_defaultClientScript" content="JavaScript">
9 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10 </HEAD>
11 <body MS_POSITIONING="GridLayout">
12 <form id="Form1" method="post" runat="server">
13 <asp:Image id="Image1" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 360px" runat="server"></asp:Image>
14 <asp:HyperLink id="HyperLink1" style="Z-INDEX: 105; LEFT: 304px; POSITION: absolute; TOP: 312px"
15 runat="server" NavigateUrl="../mv/index.html">大头帖</asp:HyperLink>
16 <asp:TextBox id="name" style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute; TOP: 144px" runat="server"
17 MaxLength="4"></asp:TextBox>
18 <asp:RadioButtonList id="fontType" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 184px"
19 runat="server" Width="240px">
20 <asp:ListItem Value="方正黄草简体" Selected="True">方正黄草简体</asp:ListItem>
21 <asp:ListItem Value="汉鼎繁淡古">汉鼎繁淡古</asp:ListItem>
22 <asp:ListItem Value="汉鼎繁印篆">汉鼎繁印篆</asp:ListItem>
23 <asp:ListItem Value="经典繁桟亭">经典繁桟亭</asp:ListItem>
24 </asp:RadioButtonList>
25 <asp:Button id="bu" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 312px" runat="server"
26 Text=" 刻 字 "></asp:Button>
27 </form>
28 </body>
29</HTML>
30
CS文件:
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.IO;
8using System.Web;
9using System.Web.SessionState;
10using System.Web.UI;
11using System.Web.UI.WebControls;
12using System.Web.UI.HtmlControls;
13
14namespace testItem.movie
15{
16 /// <summary>
17 /// 生成印章签名
18 /// </summary>
19 public class underWrite : System.Web.UI.Page
20 {
21 protected System.Web.UI.WebControls.Image Image1;
22 protected System.Web.UI.WebControls.RadioButtonList fontType;
23 protected System.Web.UI.WebControls.HyperLink HyperLink1;
24 protected System.Web.UI.WebControls.Button bu;
25 protected System.Web.UI.WebControls.TextBox name;
26
27 private void Page_Load(object sender, System.EventArgs e)
28 {
29 Image1.Visible = false;
30 }
31
32 Web 窗体设计器生成的代码
53
54 private void Button1_Click(object sender, System.EventArgs e)
55 {
56 string Name = name.Text;
57 if ( Name.Length < 4 ) {
58 Response.Write( "请输入最少4个字符!" );
59 return;
60 }
61
62 string FontName = fontType.SelectedValue;
63
64 int fontSize = 36;
65 int x = 10;
66 int y = 10;
67
68 switch( FontName ) {
69 case "方正黄草简体":
70 x = -8;
71 y = -2;
72 fontSize = 42;
73 break;
74
75 case "汉鼎繁淡古":
76 x = -10;
77 y = -2;
78 break;
79
80 case "汉鼎繁印篆":
81 x = -15;
82 y = -2;
83 break;
84
85 case "经典繁桟亭":
86 fontSize = 34;
87 x = -10;
88 y = -3;
89 break;
90 }
91
92 Bitmap bm = new Bitmap(110,100,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
93 Font font = new Font(FontName,fontSize,FontStyle.Bold);
94
95 Graphics g = Graphics.FromImage( bm );
96
97 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
98
99 /* 这里的三个属性可以根据情况开放.
100 *
101 //g.CompositingQuality = CompositingQuality.HighQuality;
102 //g.SmoothingMode = SmoothingMode.HighQuality;
103 //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
104
105 */
106 g.Clear(Color.Red); //赋予图像一个背景色
107
108 g.DrawString( Name.Substring(2,1),font,new SolidBrush(Color.White),0,2 );
109 g.DrawString( Name.Substring(3,1),font,new SolidBrush(Color.White),0,45-y );
110 g.DrawString( Name.Substring(0,1),font,new SolidBrush(Color.White),35-x,2 );
111 g.DrawString( Name.Substring(1,1),font,new SolidBrush(Color.White),35-x,45-y );
112 g.Dispose();
113 //设置保存路径
114 bm.Save( Server.MapPath("../upload/c.jpg"),System.Drawing.Imaging.ImageFormat.Jpeg );
115 bm.Dispose();
116 //显示产生的图片
117 Image1.ImageUrl = Server.MapPath("../upload/c.jpg") ;
118 Image1.Visible = true;
119 }
120 }
121}
122
效果: