一个图片放大镜_如云随风而动_百度空间
代码写的不好,不过功能实现了o(∩_∩)o...哈哈
代码提供给大家

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

namespace Magnifier
{
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.ComponentModel.Container components = null;
  private Image img = null;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.Panel panel2;
  private System.Windows.Forms.PictureBox pictureBox2;
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.Label label1;
  private int scale = 1;

  public Form1()
  {
   InitializeComponent();
  }

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

  #region Windows 窗体设计器生成的代码
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.panel1 = new System.Windows.Forms.Panel();
   this.panel2 = new System.Windows.Forms.Panel();
   this.pictureBox2 = new System.Windows.Forms.PictureBox();
   this.pictureBox1 = new System.Windows.Forms.PictureBox();
   this.label1 = new System.Windows.Forms.Label();
   this.panel1.SuspendLayout();
   this.panel2.SuspendLayout();
   this.SuspendLayout();
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(416, 520);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(96, 23);
   this.button1.TabIndex = 1;
   this.button1.Text = "浏览 >>";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // panel1
   // 
   this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.panel1.Controls.Add(this.panel2);
   this.panel1.Controls.Add(this.pictureBox1);
   this.panel1.Location = new System.Drawing.Point(8, 8);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(504, 504);
   this.panel1.TabIndex = 0;
   // 
   // panel2
   // 
   this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
   this.panel2.Controls.Add(this.pictureBox2);
   this.panel2.Enabled = false;
   this.panel2.Location = new System.Drawing.Point(50, 50);
   this.panel2.Name = "panel2";
   this.panel2.Size = new System.Drawing.Size(80, 80);
   this.panel2.TabIndex = 1;
   // 
   // pictureBox2
   // 
   this.pictureBox2.Location = new System.Drawing.Point(0, 0);
   this.pictureBox2.Name = "pictureBox2";
   this.pictureBox2.Size = new System.Drawing.Size(80, 80);
   this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
   this.pictureBox2.TabIndex = 0;
   this.pictureBox2.TabStop = false;
   // 
   // pictureBox1
   // 
   this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Cross;
   this.pictureBox1.Location = new System.Drawing.Point(0, 0);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(500, 500);
   this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
   this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_DoubleClick);
   this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
   // 
   // label1
   // 
   this.label1.ForeColor = System.Drawing.Color.Blue;
   this.label1.Location = new System.Drawing.Point(8, 520);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(296, 24);
   this.label1.TabIndex = 2;
   this.label1.Text = "说明:鼠标单击可放大图片,双击可还原初始大小。";
   this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(522, 551);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.panel1);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
   this.MaximizeBox = false;
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "图片放大镜 - 测试版 http://www.hbhmao.cn";
   this.panel1.ResumeLayout(false);
   this.panel2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  [STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   openFileDialog1.InitialDirectory = Application.StartupPath;
   openFileDialog1.Filter = "图片文件|*.jpg;*.bmp;*.gif;*.jpeg;*.png";
   if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK)
   {
    return;
   }
   pictureBox1.Width = pictureBox1.Height = 500;
   try
   {
    img = Image.FromFile(openFileDialog1.FileName);
    if (img.Width < img.Height)
    {
     pictureBox1.Width = (int)((1.0 * img.Width / img.Height) * pictureBox1.Height);
    }
    else
    {
     pictureBox1.Height = (int)((1.0 * img.Height / img.Width) * pictureBox1.Width);
    }
    pictureBox1.Image = img;
    pictureBox2.Width = img.Width * scale;
    pictureBox2.Height = img.Height * scale;
    pictureBox2.Image = img;
   }
   catch (OutOfMemoryException ee)
   {
    MessageBox.Show("文件类型不对"+ee);
   }
   catch (Exception ee)
   {
    MessageBox.Show("不知名错误"+ee);
   }
  }

  private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if (img != null)
   {
    int left = (int)(e.X * (1.0 * img.Width/pictureBox1.Width)*scale);
    int top = (int)(e.Y * (1.0 * img.Height/pictureBox1.Height)*scale);

    left -= panel2.Width/2;
    top -= panel2.Height/2;

    pictureBox2.Left = -left;
    pictureBox2.Top = -top;

    pictureBox1.Refresh();
   }
   panel2.Top = e.Y - panel2.Height / 2;
   panel2.Left = e.X - panel2.Width / 2;
  }

  private void pictureBox1_Click(object sender, System.EventArgs e)
  {
   if (img == null || scale > 10)
   {
    return;
   }
   scale++;
   panel2.Width += 10;
   panel2.Height += 10;
   pictureBox2.Width = img.Width * scale;
   pictureBox2.Height = img.Height * scale;
  }

  private void pictureBox1_DoubleClick(object sender, System.EventArgs e)
  {
   if (img == null)
   {
    return;
   }
   scale = 1;
   panel2.Width = 80;
   panel2.Height = 80;
   pictureBox2.Width = img.Width * scale;
   pictureBox2.Height = img.Height * scale;
  }

 }
}
转:


郑重声明:资讯 【一个图片放大镜_如云随风而动_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——