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

The author:(作者)qq
published in(发表于) 2014/7/11 9:23:43
C#教程:在GDI+中缩放图像

C#教程:在GDI+中缩放图像

在GDI+中缩放图像

DrawImage为可重载方法,它的一种形式接收一个Bitmap对象和一个Rectangle结构。Rectangle矩形结构指定了绘图操作的目标,如果目标矩形的大小与原始图像的大小不同,原始图像将进行缩放,以适应目标矩形。

示例

缩放图像

本示例中,当程序运行时,单击【缩放图像】按钮,在窗体中从左向右依次绘制3幅图像,其中第1幅图为原始图像的缩小形式,第2幅图与原始图像大小相同,第3幅图为原始图像的放大形式。示例运行结果如图1所示。



图1 缩放图像

Form1窗体中,在【缩放图像】按钮的Click事件中定义3个Rectangle矩形结构,然后调用Graphics类的DrawImage方法,在窗体中根据已经定义的3个Rectangle矩形结构依次绘制3幅图像。【缩放图像】按钮的Click事件代码如下:

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

Bitmap bitmap = new Bitmap("image.jpg");

Rectangle rectangle1 = new Rectangle(10, 80, 100, 60);

Rectangle rectangle2 = new Rectangle(130, 30, 283, 198);

Rectangle rectangle3 = new Rectangle(435, 20, 300, 210);

graphics.DrawImage(bitmap, rectangle1);

graphics.DrawImage(bitmap, rectangle2);

graphics.DrawImage(bitmap, rectangle3);

}

完整程序代码如下:

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

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace _6_19

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Graphics graphics = this.CreateGraphics();

Bitmap bitmap = new Bitmap("image.jpg");

Rectangle rectangle1 = new Rectangle(10, 80, 100, 60);

Rectangle rectangle2 = new Rectangle(130, 30, 283, 198);

Rectangle rectangle3 = new Rectangle(435, 20, 300, 210);

graphics.DrawImage(bitmap, rectangle1);

graphics.DrawImage(bitmap, rectangle2);

graphics.DrawImage(bitmap, rectangle3);

}

}

}

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

namespace _6_19

{

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.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(24, 12);

this.button1.Name = "button1";

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

this.button1.TabIndex = 0;

this.button1.Text = "缩放图像";

this.button1.UseVisualStyleBackColor = true;

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

//

// Form1

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(746, 249);

this.Controls.Add(this.button1);

this.MaximizeBox = false;

this.Name = "Form1";

this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;

}

}

★ ★★★★Program.cs主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace _6_19

{

static class Program

{

///


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





QQ:154298438
QQ:417480759