比如图片比较大 或者经过放大后超出了picturebox的边框 怎么拖动图片来看的比较舒服或者看到到被遮住的部分

解决方案 »

  1.   

    请注意,不是拖动pictureBox,谢谢
      

  2.   

    你可以通过继承PictureBox来达到这一点。
      

  3.   

    你可以参考下面的代码来自己编写一个控件:using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Data;namespace PictureBoxApp
    {
    public class ImageCtr : ScrollableControl
    {
    private Image m_Img; public Image Img
    {
    get { return m_Img; }
    set
    {
    if (value != this.m_Img)
    {
    m_Img = value;
    if (this.m_Img != null)
    {
    this.AutoScrollMinSize = this.m_Img.Size;
    }
    else
    {
    this.AutoScrollMinSize = Size.Empty;
    }
    this.Invalidate();
    }
    }
    }
    public ImageCtr()
    {
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    if (this.m_Img != null)
    {
    e.Graphics.DrawImage(this.m_Img, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.m_Img.Width, this.m_Img.Height);
    }
    }
    protected override void OnPrint(PaintEventArgs e)
    {
    if (this.m_Img != null)
    {
    e.Graphics.DrawImage(this.m_Img, 0, 0, this.m_Img.Width, this.m_Img.Height);
    }
    }
    }
    }
      

  4.   

    直接把picturebox控件的SizeMode属性修改为StretchImage
      

  5.   

    多谢各位,还是通过重绘解决了问题
     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;
            }