COPY程序后在pictureBox/image属性添加一个图片就可以运行了,源程序:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button move;
private System.Windows.Forms.Button jubu;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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.btnReset = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.move = new System.Windows.Forms.Button();
this.jubu = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
// 
// pictureBox1
// 
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(12, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(570, 360);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
// 
// btnReset
// 
this.btnReset.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnReset.Location = new System.Drawing.Point(660, 8);
this.btnReset.Name = "btnReset";
this.btnReset.TabIndex = 5;
this.btnReset.Text = "恢复";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
// 
// panel1
// 
this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right)));
this.panel1.AutoScroll = true;
this.panel1.BackColor = System.Drawing.SystemColors.Window;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(12, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(636, 444);
this.panel1.TabIndex = 6;
// 
// move
// 
this.move.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.move.Location = new System.Drawing.Point(660, 48);
this.move.Name = "move";
this.move.TabIndex = 5;
this.move.Text = "移动";
this.move.Click += new System.EventHandler(this.move_Click);
// 
// jubu
// 
this.jubu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.jubu.Location = new System.Drawing.Point(660, 88);
this.jubu.Name = "jubu";
this.jubu.TabIndex = 5;
this.jubu.Text = "局部放大";
this.jubu.Click += new System.EventHandler(this.jubu_Click);
// 
// Form1
// 
this.AutoScale = false;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(740, 462);
this.Controls.Add(this.panel1);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.move);
this.Controls.Add(this.jubu);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion

解决方案 »

  1.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    Application.Run(new Form1());
    } [ System.Runtime.InteropServices.DllImportAttribute ( "user32.dll" ) ]
    private static extern bool ClipCursor(ref Rectangle rect);
    private Point start = new Point(0, 0);
    private Point end = new Point(0, 0);
    private bool down = false;
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    down = true;
    start = new Point(e.X, e.Y);
    end = new Point(e.X, e.Y);
    Rectangle rect = ClientToScreen(pictureBox1);
    ClipCursor(ref rect);
    } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(down)
    {
    pictureBox1.Invalidate();
    end = new Point(e.X, e.Y);
    }
    } private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(down&&(imgRect.Width < 84000))
    {
    Rectangle rect = Screen.GetBounds(this);
    ClipCursor(ref rect);
    down = false;
    end = new Point(e.X, e.Y);
    CalcRect(GetRect());
    pictureBox1.Invalidate();
    }
    }
    private Rectangle GetRect()
    {
    Rectangle rect = new Rectangle(0, 0, 0, 0);
    rect.X = Math.Min(start.X, end.X);
    rect.Y = Math.Min(start.Y, end.Y);
    rect.Width = Math.Abs(start.X - end.X);
    rect.Height = Math.Abs(start.Y - end.Y);
    return rect;
    }
    private Rectangle ClientToScreen(Control control)
    {
    Point pos = control.PointToScreen(control.Location);
    pos.X -= control.Location.X;
    pos.Y -= control.Location.Y;
    return new Rectangle(pos, new Size(control.ClientRectangle.Width + pos.X, control.ClientRectangle.Height + pos.Y));

    } private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    e.Graphics.DrawImage(pictureBox1.Image, imgRect);
    if(down)
    {
    Pen pen = new Pen(Color.Blue, 0.1f);
    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
    e.Graphics.DrawRectangle(pen, GetRect());
    }
    }

    private void CalcRect(RectangleF SelectedRect)
    {
    if(imgRect.Width > pictureBox1.Image.Width * 20)
    {
    MessageBox.Show("已经超过20倍了!");
    btnReset_Click(null, new EventArgs());
    return;
    }
    float w_scale = (float)panel1.ClientRectangle.Width / (float)SelectedRect.Width;
    float h_scale = (float)panel1.ClientRectangle.Height / (float)SelectedRect.Height;
    if(w_scale > h_scale)
    {
    float tmp = (float)panel1.ClientRectangle.Width / h_scale;
    SelectedRect.X -= (tmp - SelectedRect.Width) / 2;
    SelectedRect.Width = tmp;
    w_scale = h_scale;
    }
    else
    {
    float tmp = (float)panel1.ClientRectangle.Height / w_scale;
    SelectedRect.Y -= (tmp - SelectedRect.Height) / 2;
    SelectedRect.Height = tmp;
    }
    SelectedRect.X -= imgRect.X;
    SelectedRect.Y -= imgRect.Y; imgRect.Width *= w_scale;
    imgRect.Height *= w_scale; pictureBox1.Size = new Size((int)Math.Round(imgRect.Width), (int)Math.Round(imgRect.Height));
    this.pictureBox1.Left = - (int)Math.Round(SelectedRect.X * w_scale);
    this.pictureBox1.Top = - (int)Math.Round(SelectedRect.Y * w_scale);
    Text = "显示比例:" + (Math.Round(imgRect.Width * 100 / pictureBox1.Image.Width)).ToString() + "%";
    } private RectangleF imgRect;
    private void Form1_Load(object sender, System.EventArgs e)
    {
    btnReset_Click(null, new EventArgs());
    } private void btnReset_Click(object sender, System.EventArgs e)
    {
    pictureBox1.Location = new Point(0, 0);
    pictureBox1.Size = pictureBox1.Image.Size;
    pictureBox1.CreateGraphics().DrawImage(pictureBox1.Image, 0, 0);
    imgRect = new RectangleF(0, 0, pictureBox1.Image.Width, pictureBox1.Image.Height);
    Text = "显示比例:" + (Math.Round(imgRect.Width * 100 / pictureBox1.Image.Width)).ToString() + "%";
    } private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
    MessageBox.Show("已经超过规定的倍数了!");return;
    } private void move_Click(object sender, System.EventArgs e)
    {
    pictureBox1.Cursor = Cursors.Hand ;
    } private void jubu_Click(object sender, System.EventArgs e)
    {
    pictureBox1.Cursor = Cursors.Cross ;
    }
    }
    }
      

  2.   

    这样用鼠标移动
    private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(pictureBox1.Cursor==Cursors.Hand)
    {
    if(e.Button==MouseButtons.Left)
    {
    p=new Point(e.X,e.Y);
    }
    }
    else
    {
    down = true;
    start = new Point(e.X, e.Y);
    end = new Point(e.X, e.Y);
    Rectangle rect = ClientToScreen(pictureBox1);
    ClipCursor(ref rect);
    }
    } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(pictureBox1.Cursor==Cursors.Hand)
    {
    if(e.Button==MouseButtons.Left)
    {
    this.pictureBox1.Top=this.pictureBox1.Top+e.Y-p.Y;
                        this.pictureBox1.Left=this.pictureBox1.Left+e.X-p.X;
    }
    }
    else if(down)
    {
    pictureBox1.Invalidate();
    end = new Point(e.X, e.Y);
    }
    }