常用图形绘制
<%@ 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(, );
//创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
gph.SmoothingMode = SmoothingMode.HighQuality;
//设置图片质量,指定是否将平滑处理(消除锯齿)应用于直线、曲线和已填充区域的边缘
gph.Clear(Color.Red);
//清除整个绘图面并以指定背景色填充
gph.DrawRectangle(Pens.Blue, , , , );
gph.FillRectangle(Brushes.Blue, , , , );
//绘制矩形
gph.DrawEllipse(Pens.Blue, , , , );
gph.FillEllipse(Brushes.Blue, , , , );
//绘制椭圆
gph.DrawPie(Pens.Blue, , , , , , );
//绘制圆弧
gph.FillPie(Brushes.Blue, , , , , , );
//绘制饼图
Point[] line={new Point(,),new Point(,),new Point(,),new Point(,)};
gph.DrawCurve(Pens.Blue, line);
//绘制曲线
gph.DrawBezier(Pens.Blue, new Point(, ), new Point(, ), new Point(, ), new Point(, ));
//绘制贝塞尔曲线
Point[] line2 ={ new Point(, ), new Point(, ), new Point(, ), new Point(, ) };
gph.DrawPolygon(Pens.Blue, line2);
//gph.FillPolygon(Pens.Blue, line2);
//绘制多边形
Bitmap mybit=new Bitmap(Server.MapPath("001.jpg"));
gph.DrawImage(mybit,,,,);
//绘制图片
gph.DrawLine(Pens.Black, , , , );
//绘制直线
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(, );
//创建一个宽400,高200的实例
Graphics gph;
//从指定的Image对象创建新Graphics对象
gph = Graphics.FromImage(bmp);
gph.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
//设置字符串质量
gph.Clear(Color.Red);
//清除整个绘图面并以指定背景色填充
gph.DrawString("无换行显示:绘制文本换行显示的字符串,应该使用文本外部边界的长方行", new Font("宋体", ), Brushes.Blue, , );
//绘制文本字符串string, Font, Brush, float, float(字符串,字体,x,y坐标)
RectangleF rect = new RectangleF(, , , );
string str = "绘制文本换行显示的字符串,应该使用文本外部边界的长方行";
gph.DrawString(str, new Font("宋体", ), 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( , );
objGraphics = Graphics.FromImage( objBitmap );
objPen = new Pen( Color.Yellow );
objBrush = new SolidBrush( Color.Yellow );
objFont = new Font( "Lucida Sans Unicode", );
objGraphics.SmoothingMode = SmoothingMode.Default;
objGraphics.DrawString( "Default", objFont, objBrush, , );
objGraphics.DrawEllipse( objPen, , , , );
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.DrawString( "AntiAlias", objFont, objBrush, , );
objGraphics.DrawEllipse( objPen, , , , );
objGraphics.SmoothingMode = SmoothingMode.HighQuality;
objGraphics.DrawString( "HighQuality", objFont, objBrush, , );
objGraphics.DrawEllipse( objPen, , , , );
objGraphics.SmoothingMode = SmoothingMode.HighSpeed;
objGraphics.DrawString( "HighSpeed", objFont, objBrush, , );
objGraphics.DrawEllipse( objPen, , , , );
objGraphics.SmoothingMode = SmoothingMode.None;
objGraphics.DrawString( "None", objFont, objBrush, , );
objGraphics.DrawEllipse( objPen, , , , );
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( , );
objGraphics = Graphics.FromImage( objBitmap );
objGraphics.Clear( Color.DarkBlue );
Font objFont = new Font( "Times", );
strString = "ABCabc123 - AntiAlias";
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
strString = "ABCabc123 - AntiAliasGridFit";
objGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
strString = "ABCabc123 - ClearTypeGridFit";
objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
strString = "ABCabc123 - SingleBitPerPixel";
objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
strString = "ABCabc123 - SingleBitPerPixelGridFit";
objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
strString = "ABCabc123 - SystemDefault";
objGraphics.TextRenderingHint = TextRenderingHint.SystemDefault;
objGraphics.DrawString( strString, objFont, Brushes.White, , );
objBitmap.Save( Response.OutputStream, ImageFormat.Jpeg );
}
</Script>