(16条消息) 在C#中 从一个picturebox中 按住鼠标左键不放 画一块区域后,另外一个picturebox(在一个winform 窗体上有两个picturebox )上马上把对应的截取图像显

private int _x, _y;
        Image _img = null ;
        private void pictureBox1_MouseDown( object sender, MouseEventArgs e)
        {
            _img = pictureBox1.Image.Clone() as Image;
            _x = e.X;
            _y = e.Y;

}

private void pictureBox1_MouseUp( object sender, MouseEventArgs e)
        {
            int sx = _x < e.X ? _x : e.X;
            int sy = _y < e.Y ? _y : e.Y;
            int w = Math.Abs(_x - e.X);
            int h = Math.Abs(_y - e.Y);
            Graphics g = Graphics.FromHwnd(pictureBox2.Handle);
            g.Clear(pictureBox2.BackColor);
            g.DrawImage(pictureBox1.Image, new Rectangle( 0 , 0 , w, h), sx, sy, w, h, GraphicsUnit.Pixel);
        }

private void pictureBox1_MouseMove( object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Pen p = new Pen(Color.Red, 1 );
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
                int sx = _x < e.X ? _x : e.X;
                int sy = _y < e.Y ? _y : e.Y;
                int w = Math.Abs(_x - e.X);
                int h = Math.Abs(_y - e.Y);
                pictureBox1.Image = _img;
                g.DrawRectangle(p, sx, sy, w, h);
            }
        }

不过这样效果不是很好,最好是这样,你有一个image保存最原始的图片, 画虚线框在这个image的副本上画,然后赋值给pictuebox1,取图片拷贝的时候也从原始的image上取

(0)

相关推荐