(一).说明
将鼠标指向一幅图片的一块区域,此区域会放大显示,变清晰.
用类: Graphics 实现.
(二).代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace 放大图像区域
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
//private Cursor myCursor;
Cursor myCursor=new Cursor("..\\..\\MAGNIFY.cur"); //自定义鼠标
Graphics g;
Image myImage;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlText;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(24, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(440, 384);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
//
// button1
//
this.button1.Location = new System.Drawing.Point(384, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(472, 406);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Cursor.Current=myCursor;
Rectangle sourceRectangle=new Rectangle(e.X-10,e.Y-10,20,20); //要放大的区域
//Rectangle destRectangle=new Rectangle(e.X-20,e.Y-20,40,40);
Rectangle destRectangle=new Rectangle(pictureBox1.Width-150,pictureBox1.Height-150,pictureBox1.Width,pictureBox1.Height); //放大的比例
g.DrawLine(myImage,destRectangle,sourceRectangle,GraphicsUnit.Pixel);
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Cursor.Current=Cursors.Default;
}
private void Form1_Load(object sender, System.EventArgs e)
{
g=this.pictureBox1.CreateGraphics();
myImage=this.pictureBox1.Image;
}
private void button1_Click(object sender, System.EventArgs e)
{
//Graphics g=this.pictureBox1.CreateGraphics();
Graphics g=pictureBox1.CreateGraphics();
g.DrawLine(new Pen(Color.Red,5),20,20,50,50);
}
}
}
谢谢阅读!