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(600, 500);
 //创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
gph.SmoothingMode = SmoothingMode.HighQuality;
//设置图片质量,指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘
gph.Clear(Color.Red);
//清除整个绘图面并以指定背景色填充
gph.DrawRectangle(Pens.Blue, 10, 10, 100, 100);
gph.FillRectangle(Brushes.Blue, 120, 10, 100, 100);
//绘制矩形
gph.DrawEllipse(Pens.Blue, 10, 120, 100, 100);
gph.FillEllipse(Brushes.Blue, 120, 120, 100, 100);
//绘制椭圆
gph.DrawPie(Pens.Blue, 10, 230, 100, 100, 0, 270);
//绘制圆弧
gph.FillPie(Brushes.Blue, 120, 230, 100, 100, 0, 270);
//绘制饼图
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(10, 340), new Point(60, 30), new Point(110, 30), new Point(160, 340));
//绘制贝塞尔曲线
Point[] line2 ={ new Point(10, 340), new Point(340, 100), new Point(190, 340), new Point(10, 340) };
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, 10, 480, 300, 480);
//绘制直线
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(600, 500);
 //创建一个宽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, 10, 10);
//绘制文本字符串string, Font, Brush, float, float(字符串,字体,x,y坐标)
RectangleF rect = new RectangleF(10, 110, 300, 200);
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( 400, 400 );
  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, 50, 20 );
  objGraphics.DrawEllipse( objPen, 10, 10, 200, 50 );
  objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
  objGraphics.DrawString( "AntiAlias", objFont, objBrush, 50, 80 );
  objGraphics.DrawEllipse( objPen, 10, 70, 200, 50 );
  objGraphics.SmoothingMode = SmoothingMode.HighQuality;
  objGraphics.DrawString( "HighQuality", objFont, objBrush, 50, 140 );
  objGraphics.DrawEllipse( objPen, 10, 130, 200, 50 );
  objGraphics.SmoothingMode = SmoothingMode.HighSpeed;
  objGraphics.DrawString( "HighSpeed", objFont, objBrush, 50, 200 );
  objGraphics.DrawEllipse( objPen, 10, 190, 200, 50 );
  objGraphics.SmoothingMode = SmoothingMode.None;
  objGraphics.DrawString( "None", objFont, objBrush, 50, 260 );
  objGraphics.DrawEllipse( objPen, 10, 250, 200, 50 );
  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( 600, 400 );
  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, 10, 10 );
  strString = "ABCabc123 - AntiAliasGridFit";
  objGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  objGraphics.DrawString( strString, objFont, Brushes.White, 10, 50 );
  strString = "ABCabc123 - ClearTypeGridFit";
  objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  objGraphics.DrawString( strString, objFont, Brushes.White, 10, 90 );
  strString = "ABCabc123 - SingleBitPerPixel";
  objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
  objGraphics.DrawString( strString, objFont, Brushes.White, 10, 130 );
  strString = "ABCabc123 - SingleBitPerPixelGridFit";
  objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
  objGraphics.DrawString( strString, objFont, Brushes.White, 10, 170 );
  strString = "ABCabc123 - SystemDefault";
  objGraphics.TextRenderingHint = TextRenderingHint.SystemDefault;
  objGraphics.DrawString( strString, objFont, Brushes.White, 10, 210 );
  objBitmap.Save( Response.OutputStream, ImageFormat.Jpeg );
}
</Script>

 

posted @ 2014-03-30 21:46  天马3798  阅读(277)  评论(0编辑  收藏  举报