对于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页面:
1
<%
@ Page language
=
"
c#
"
Codebehind
=
"
underWrite.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
testItem.movie.underWrite
"
%>
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文件:
1
using
System;
2
using
System.Collections;
3
using
System.ComponentModel;
4
using
System.Data;
5
using
System.Drawing;
6
using
System.Drawing.Drawing2D;
7
using
System.IO;
8
using
System.Web;
9
using
System.Web.SessionState;
10
using
System.Web.UI;
11
using
System.Web.UI.WebControls;
12
using
System.Web.UI.HtmlControls;
13
![None.gif](/Images/OutliningIndicators/None.gif)
14
namespace
testItem.movie
15
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 生成印章签名
18
/// </summary>
19
public class underWrite : System.Web.UI.Page
20![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
Image1.Visible = false;
30
}
31![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
33
override protected void OnInit(EventArgs e)
34![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
//
36
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
37
//
38
InitializeComponent();
39
base.OnInit(e);
40
}
41
42![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
44
/// 此方法的内容。
45
/// </summary>
46
private void InitializeComponent()
47![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
this.bu.Click += new System.EventHandler(this.Button1_Click);
49
this.Load += new System.EventHandler(this.Page_Load);
50![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
}
52
#endregion
53![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
54
private void Button1_Click(object sender, System.EventArgs e)
55![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
string Name = name.Text;
57![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 这里的三个属性可以根据情况开放.
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
效果: