rainxiang

莺莺燕燕翠翠红红处处融融洽洽 风风雨雨花花叶叶年年暮暮朝朝

 

[转]GDI+基础(3)

常用图形绘制
<%@ Page ContentType="image/gif" Language="C#" %>
<!--ContentType设置页面类型-->
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp 
= new Bitmap(600500);
 
//创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
gph.SmoothingMode 
= SmoothingMode.HighQuality;
//设置图片质量,指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘
gph.Clear(Color.Red);
//清除整个绘图面并以指定背景色填充
gph.DrawRectangle(Pens.Blue, 1010100100);
gph.FillRectangle(Brushes.Blue, 
12010100100);
//绘制矩形
gph.DrawEllipse(Pens.Blue, 10120100100);
gph.FillEllipse(Brushes.Blue, 
120120100100);
//绘制椭圆
gph.DrawPie(Pens.Blue, 102301001000270);
//绘制圆弧
gph.FillPie(Brushes.Blue, 1202301001000270);
//绘制饼图
Point[] line={new Point(10,340),new Point(60,30),new Point(110,30),new Point(160,340)};
gph.DrawCurve(Pens.Blue, line);
//绘制曲线
gph.DrawBezier(Pens.Blue, new Point(10340), new Point(6030), new Point(11030), new Point(160340));
//绘制贝塞尔曲线
Point[] line2 =new Point(10340), new Point(340100), new Point(190340), new Point(10340) };
gph.DrawPolygon(Pens.Blue, line2);
//gph.FillPolygon(Pens.Blue, line2);
//绘制多边形
Bitmap mybit=new Bitmap(Server.MapPath("001.jpg"));
gph.DrawImage(mybit,
10,360,100,100);
//绘制图片
gph.DrawLine(Pens.Black, 10480300480);
//绘制直线
bmp.Save(Response.OutputStream, ImageFormat.Gif);//ImageFormat 对象,它指定保存的图像的格式
//向客户端输出数据流,并以此数据流形成Gif图片 
}

</script>

绘制文本字符串
<%@ Page ContentType="image/gif" Language="C#" %>
<!--ContentType设置页面类型-->
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<%@ Import namespace="System.Drawing.Text" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp 
= new Bitmap(600500);
 
//创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
gph.TextRenderingHint 
= TextRenderingHint.ClearTypeGridFit;
//设置字符串质量
gph.Clear(Color.Red);
//清除整个绘图面并以指定背景色填充
gph.DrawString("无换行显示:绘制文本换行显示的字符串,应该使用文本外部边界的长方行"new Font("宋体"16), Brushes.Blue, 1010);
//绘制文本字符串string, Font, Brush, float, float(字符串,字体,x,y坐标)
RectangleF rect = new RectangleF(10110300200);
string str = "绘制文本换行显示的字符串,应该使用文本外部边界的长方行";
gph.DrawString(str, 
new Font("宋体"16), Brushes.Blue,rect);
//绘制有范围的字符串
bmp.Save(Response.OutputStream, ImageFormat.Gif);//ImageFormat 对象,它指定保存的图像的格式
//向客户端输出数据流,并以此数据流形成Gif图片 
}

</script>

设置图片质量

SmoothingMode 枚举 指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘

成员名称 说明
AntiAlias 指定消除锯齿的呈现。
Default 指定默认模式。
HighQuality 指定高质量、低速度呈现。
HighSpeed 指定高速度、低质量呈现。
Invalid 指定一个无效模式。
None 指定不消除锯齿。
<%@ ContentType="image/jpeg" Language="C#" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Drawing2D" %>
<%@ Import namespace="System.Drawing.Text" %>
<script language="C#" runat=server>
void Page_Load(Object sender , EventArgs e) 
{
  Bitmap objBitmap;
  Graphics objGraphics;
  Pen objPen;
  Brush objBrush;
  Font objFont;
  objBitmap 
= new Bitmap( 400400 );
  objGraphics 
= Graphics.FromImage( objBitmap );
  objPen 
= new Pen( Color.Yellow );
  objBrush 
= new SolidBrush( Color.Yellow );
  objFont 
= new Font( "Lucida Sans Unicode"18 );
  objGraphics.SmoothingMode 
= SmoothingMode.Default;
  objGraphics.DrawString( 
"Default", objFont, objBrush, 5020 );
  objGraphics.DrawEllipse( objPen, 
101020050 );
  objGraphics.SmoothingMode 
= SmoothingMode.AntiAlias;
  objGraphics.DrawString( 
"AntiAlias", objFont, objBrush, 5080 );
  objGraphics.DrawEllipse( objPen, 
107020050 );
  objGraphics.SmoothingMode 
= SmoothingMode.HighQuality;
  objGraphics.DrawString( 
"HighQuality", objFont, objBrush, 50140 );
  objGraphics.DrawEllipse( objPen, 
1013020050 );
  objGraphics.SmoothingMode 
= SmoothingMode.HighSpeed;
  objGraphics.DrawString( 
"HighSpeed", objFont, objBrush, 50200 );
  objGraphics.DrawEllipse( objPen, 
1019020050 );
  objGraphics.SmoothingMode 
= SmoothingMode.None;
  objGraphics.DrawString( 
"None", objFont, objBrush, 50260 );
  objGraphics.DrawEllipse( objPen, 
1025020050 );
  objBitmap.Save( Response.OutputStream, ImageFormat.Jpeg );
}

</Script>

设置文本质量
TextRenderingHint 枚举 指定文本呈现的质量

成员名称 说明
AntiAlias 指定在无提示的情况下使用每个字符的 AntiAlias 标志符号位图来绘制字符。由于采用了 AntiAlias,质量会得到改善。由于关闭了提示,主干宽度差可能会比较明显。
AntiAliasGridFit 指定在有提示的情况下使用每个字符的 AntiAlias 标志符号位图来绘制字符。由于采用了 AntiAlias,质量会得到大大改善,但同时会增加性能成本。
ClearTypeGridFit 指定在有提示的情况下使用每个字符的标志符号 CT 位图来绘制字符。这是质量最高的设置。用于利用 ClearType 字体功能。
SingleBitPerPixel 指定使用每个字符的标志符号位图来绘制字符。不使用提示。
SingleBitPerPixelGridFit 指定使用每个字符的标志符号位图来绘制字符。提示用于改善字符在主干和弯曲部分的外观。
SystemDefault 指定在有系统默认呈现提示的情况下使用每个字符的标志符号位图来绘制字符。将采用用户为系统选择的所有字体修匀设置来绘制文本。
<%@ Page ContentType="image/jpeg" Language="C#" %>
<%@ Import namespace="System.Drawing" %>
<%@ Import namespace="System.Drawing.Imaging" %>
<%@ Import namespace="System.Drawing.Text" %>
<script language="C#" runat=server>
void Page_Load(Object sender , EventArgs e) 
{
  Bitmap objBitmap;
  Graphics objGraphics;
  
string strString;
  objBitmap 
= new Bitmap( 600400 );
  objGraphics 
= Graphics.FromImage( objBitmap );
  objGraphics.Clear( Color.DarkBlue );
  Font objFont 
= new Font( "Times"24 );
  strString 
= "ABCabc123 - AntiAlias";
  objGraphics.TextRenderingHint 
= TextRenderingHint.AntiAlias;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
1010 );
  strString 
= "ABCabc123 - AntiAliasGridFit";
  objGraphics.TextRenderingHint 
= TextRenderingHint.AntiAliasGridFit;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
1050 );
  strString 
= "ABCabc123 - ClearTypeGridFit";
  objGraphics.TextRenderingHint 
= TextRenderingHint.ClearTypeGridFit;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
1090 );
  strString 
= "ABCabc123 - SingleBitPerPixel";
  objGraphics.TextRenderingHint 
= TextRenderingHint.SingleBitPerPixel;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
10130 );
  strString 
= "ABCabc123 - SingleBitPerPixelGridFit";
  objGraphics.TextRenderingHint 
= TextRenderingHint.SingleBitPerPixel;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
10170 );
  strString 
= "ABCabc123 - SystemDefault";
  objGraphics.TextRenderingHint 
= TextRenderingHint.SystemDefault;
  objGraphics.DrawString( strString, objFont, Brushes.White, 
10210 );
  objBitmap.Save( Response.OutputStream, ImageFormat.Jpeg );
}

</Script>

posted on 2006-03-23 09:13  rainxiang  阅读(4547)  评论(1编辑  收藏  举报

导航