日期:2014-05-18  浏览次数:20985 次

c# picturebox控件的放大问题求教!
各位大侠:
  小弟最近学习使用picturebox控件,画完图后,想实现缩放功能;
  具体缩放要求为:鼠标框选目标区域,自动实现放大;右键点击返回原始大小。
   
  有人说该用两个picturebox控件来实现;若是,我不懂第二个picturebox控件的位置,大小该怎么调整呢?
  若不是,我该怎么来实现呢?
  小弟尝试了几种方法,效果都不行。
   
  还请懂行的大哥给个提示,或者代码更好了!
  提示的话,最好烦请您把每一步都点明,不胜感激!
 

------解决方案--------------------
用两个PictureBox最好。
大小属性设置为 AutoScale
------解决方案--------------------
以下代码粘帖就能工作(要记得改变图象路径)。
其中,
DrawReversibleFrame:画选择框
l=... t=... : 计算当前选择影射到图像的坐标
this.Invalidate:刷新当前窗口

C# code

public partial class Form1 : Form
{
    Bitmap bitmap = new Bitmap(@"c:\temp\penguins.jpg");
    RectangleF sourceRectange;
    Point lastMouseDown;
    Point lastMouseMove;
    public Form1()
    {
        InitializeComponent();
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
        this.lastMouseDown = this.lastMouseMove = e.Location;
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if( !this.Capture) return;
        DrawReversibleFrame();
        lastMouseMove = e.Location;
        DrawReversibleFrame();
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            float l = sourceRectange.Left + sourceRectange.Width * lastMouseDown.X / this.ClientRectangle.Width;
            float t = sourceRectange.Top + sourceRectange.Height * lastMouseDown.Y / this.ClientRectangle.Height;
            float r = sourceRectange.Left + sourceRectange.Width * e.X / this.ClientRectangle.Width;
            float b = sourceRectange.Top + sourceRectange.Height * e.Y / this.ClientRectangle.Height;
            sourceRectange = RectangleF.FromLTRB(l, t, r, b);
        }
        else
        {
            this.sourceRectange = new RectangleF(PointF.Empty, bitmap.Size);
        }
        DrawReversibleFrame();
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if ( Math.Abs(sourceRectange.Width * sourceRectange.Height) < 1) sourceRectange = new RectangleF(PointF.Empty, bitmap.Size);
        e.Graphics.DrawImage(bitmap, (RectangleF)ClientRectangle, sourceRectange, GraphicsUnit.Pixel);
    }
    private void DrawReversibleFrame()
    {
        Rectangle rect = this.RectangleToScreen(Rectangle.FromLTRB(lastMouseMove.X, lastMouseMove.Y, lastMouseDown.X, lastMouseDown.Y));
        ControlPaint.DrawReversibleFrame(rect, Color.Cyan, FrameStyle.Dashed);
    }
}

------解决方案--------------------
http://blog.csdn.net/happy09li/article/details/7056567

参考
------解决方案--------------------
this.invaldate(true); 里面最好加个true参数