Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:16:55
c#中GDI+图形图像:用直线和曲线消除锯齿

c#中GDI+图形图像:用直线和曲线消除锯齿

用直线和曲线消除锯齿

1.用直线消除锯齿

用基本的DrawLine方法绘制出来的直线看上去带有锯齿,有点像楼梯,这种用楼梯状来表示直线的技术被称为锯齿化,楼梯是理论直线的一个别名。

一项更为复杂的呈现直线的技术需要使用部分透明的像素和不透明的像素,而这种呈现方式被称为消除锯齿,它可以生成视觉上更平滑的直线。消除锯齿需要使用Graphics类的SmoothingMode属性,该属性属于System.Drawing.Drawing2D命名空间,它主要用来获取或设置Graphics图像的呈现质量,其属性值及说明如表1所示。



表1 SmoothingMode属性值及说明

示例

用直线消除锯齿

本示例中,当程序运行时,单击【原图】按钮,调用DrawLine方法绘制一条直线,然后单击【直线消除锯齿】按钮,在窗体中绘制一条经过消除锯齿的直线,比较它们的呈现效果。示例运行结果如图1所示。



图1 用直线消除锯齿

Form1窗体中,单击【直线消除锯齿】按钮,调用Graphics对象的DrawLine方法在窗体中绘制一条直线,并通过设置Graphics对象的SmoothingMode属性,对绘制的直线进行消除锯齿操作。网站源代码【直线消除锯齿】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

Pen myPen = new Pen(Color.Blue, 2);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.DrawLine(myPen, 140, 40, 220, 40);

}

完整程序代码如下:

★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace _6_15

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

Pen myPen = new Pen(Color.Blue, 2);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.DrawLine(myPen, 140, 40, 220, 40);

}

private void button2_Click(object sender, EventArgs e)

{//本教程来自:HTTP://www.isstudy.com

Graphics graphics = this.CreateGraphics();

Pen myPen = new Pen(Color.Blue, 2);

graphics.DrawLine(myPen, 40, 40, 120, 40);

}

}

}

★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码★★★★★

namespace _6_15

{

partial class Form1

{

///



/// 必需的设计器变量。

///


private System.ComponentModel.IContainer components = null;

///

/// 清理所有正在使用的资源。

///


/// 如果应释放托管资源,为 true;否则为 false。

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

///


private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(138, 70);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(90, 23);

this.button1.TabIndex = 0;

this.button1.Text = "直线消除锯齿";

this.button1.UseVisualStyleBackColor = true;

this.button1.Click += new System.EventHandler(this.button1_Click);

//本教程来自:HTTP://www.isstudy.com

// button2

//

this.button2.Location = new System.Drawing.Point(44, 70);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(75, 23);

this.button2.TabIndex = 1;

this.button2.Text = "原图";

this.button2.UseVisualStyleBackColor = true;


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759