using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; namespace 图片拖动 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Bitmap m_Image = (Bitmap)Image.FromFile(@"2009120459102338.jpg"); private Point m_StarPoint = Point.Empty; private Point m_ViewPoint = Point.Empty; private bool m_StarMove = false; private void Form1_Load(object sender, EventArgs e) { pictureBox1.Image = m_Image.Clone(new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height), PixelFormat.Format24bppRgb); } //private void pictureBox1_MouseDown(object sender, MouseEventArgs e) //{ // Cursor = Cursors.Hand; // m_StarMove = true; // m_StarPoint = e.Location; //} //private void pictureBox1_MouseMove(object sender, MouseEventArgs e) //{ // if (m_StarMove) // { // // System.Threading.Thread.Sleep(100); // LoadImage(new Point(m_StarPoint.X - e.X, m_StarPoint.Y - e.Y)); // } //} //private void LoadImage(Point m_MovePoint) //{ // int _X = m_MovePoint.X + m_ViewPoint.X; // int _Y = m_MovePoint.Y + m_ViewPoint.Y; // if (_X < 0) _X = 0; // if (_Y < 0) _Y = 0; // if (_X + pictureBox1.Width > m_Image.Width) _X = m_Image.Width - pictureBox1.Width; // if (_Y + pictureBox1.Height > m_Image.Height) _Y = m_Image.Height - pictureBox1.Height; // pictureBox1.Image = m_Image.Clone(new RectangleF(_X, _Y, pictureBox1.Width, pictureBox1.Height), PixelFormat.Format24bppRgb); // m_ViewPoint.X = _X; // m_ViewPoint.Y = _Y; // pictureBox1.Refresh(); //} //private void pictureBox1_MouseUp(object sender, MouseEventArgs e) //{ // m_StarMove = false; //} bool wselected = false; Point p = new Point(); private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { wselected = true; p.X = e.X; p.Y = e.Y; } int driftX = 0, driftY = 0; int mx = 0, my = 0; private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (wselected) { driftX = p.X - e.X; driftY = p.Y - e.Y; mx = mx - driftX; my = my - driftY; Bitmap bm = new Bitmap(this.pictureBox1.Image); Graphics g = pictureBox1.CreateGraphics(); g.Clear(pictureBox1.BackColor); g.DrawImage(bm, mx, my); p.X = e.X; p.Y = e.Y; bm.Dispose(); g.Dispose(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { wselected = false; } } }