1.用直线消除锯齿 用基本的DrawLine方法绘制出来的直线看上去带有锯齿,有点像楼梯,这种用楼梯状来表示直线的技术被称为锯齿化,楼梯是理论直线的一个别名。 一项更为复杂的呈现直线的技术需要使用部分透明的像素和不透明的像素,而这种呈现方式被称为消除锯齿,它可以生成视觉上更平滑的直线。消除锯齿需要使用Graphics类的SmoothingMode属性,该属性属于System.Drawing.Drawing2D命名空间,它主要用来获取或设置Graphics图像的呈现质量,其属性值及说明如下表所示。 表 SmoothingMode属性值及说明 属性值 | 说明 | AntiAlias | 指定消除锯齿的呈现 | Default | 指定不消除锯齿 | HighQuality | 指定高质量、低速度呈现 | HighSpeed | 指定高速度、低质量呈现 | Invalid | 指定一个无效模式 | None | 指定不消除锯齿 |
本例实现的是当程序运行的时候,分别绘制两条直线,在Web窗体中绘制两条经过消除锯齿的直线,比较它们的呈现效果。 程序代码如下: Web窗体中,绘制两条条直线,并通过设置Graphics对象的SmoothingMode属性,对绘制的直线进行消除锯齿操作,代码如下。 protected void Page_Load(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(400, 150); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); Pen myPen = new Pen(Color.Blue, 2); g.DrawLine(myPen, 40, 40, 320, 40); g.DrawString("无锯齿直线", new Font("宋体", 12), new SolidBrush(Color.Red), new PointF(40,20)); g.DrawString("有锯齿直线", new Font("宋体", 12), new SolidBrush(Color.Red), new PointF(40, 80)); g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawLine(myPen, 40, 100, 320, 100); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } | 2.用曲线消除锯齿 用曲线消除锯齿操作的实现方法同用直线消除锯齿操作的方法类似,也需要用到Graphics类的SmoothingMode属性。 交叉链接:SmoothingMode属性的详细介绍请参见上节“用直线消除锯齿”。 示例 用曲线消除锯齿 本例实现的是当程序运行的时候,绘制一个椭圆,一个经过消除锯齿的椭圆,比较它们的呈现效果。 程序代码如下: Web窗体中,调用Graphics对象的DrawEllipse方法在Web窗体中绘制两个椭圆,并通过设置Graphics对象的SmoothingMode属性,对绘制的椭圆进行消除锯齿操作,代码如下。 protected void Page_Load(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(400, 150); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); //绘制 有锯齿椭圆 Pen myPen = new Pen(Color.Blue, 2); g.DrawEllipse(myPen, 110, 20, 120, 50); g.DrawString("有锯齿椭圆", new Font("宋体", 12), new SolidBrush(Color.Red), new PointF(20, 20)); //绘制 有锯齿椭圆 Rectangle myRectangle = new Rectangle(110, 80, 120, 50); g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawEllipse(myPen, myRectangle); g.DrawString("无锯齿椭圆", new Font("宋体", 12), new SolidBrush(Color.Red), new PointF(20, 80)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); } |
|