Control Study -> 当鼠标在图片上移动时,放大图像区域

来源:互联网 发布:java工程师证书 编辑:程序博客网 时间:2024/05/14 01:54

(一).说明

  将鼠标指向一幅图片的一块区域,此区域会放大显示,变清晰.
用类: Graphics 实现.

(二).图片示例

  

(三).代码  

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace 放大图像区域
{
 ///


 /// Form1 的摘要说明。
 ///

 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.PictureBox pictureBox1;
  ///
  /// 必需的设计器变量。
  ///

  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 调用后添加任何构造函数代码
   //
  }

  ///


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

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

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


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  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

  ///


  /// 应用程序的主入口点。
  ///

  [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.DrawImage(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);
  }
 }
}

(四).示例下载

   http://www.cnblogs.com/Files/ChengKing/放大图像区域.rar

 




Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=496680


原创粉丝点击