日期:2014-05-19  浏览次数:20857 次

如何在picturebox中建立一个可移动的矩形选框,并在另一个picturebox中显示矩形选框所选原图形部分?在线跪求
如何在picturebox中建立一个可移动的矩形选框,并在另一个picturebox中显示矩形选框所选原图形部分?

------解决方案--------------------
获取部分,我的抓屏是简单的VB.NEt代码,自己改写

Dim CurImage As Image = New Bitmap(DrawRectangle.Width, DrawRectangle.Height)
Dim g As Graphics = Graphics.FromImage(CurImage)
Dim FromRectangle As Rectangle
Dim ToRectangle As Rectangle
'===========================================
FromRectangle = New Rectangle(DrawRectangle.X, DrawRectangle.Y, DrawRectangle.Width, DrawRectangle.Height)
ToRectangle = New Rectangle(0, 0, DrawRectangle.Width, DrawRectangle.Height)
g.Clear(Color.White)
'PicBox.Image, ToRectangle, FromRectangle
'分别是:截取可能和目标图形不是一样大
'原有图形,目标图形的区域 <就是在你设定的图形里面的位置> ,原有图形的区域
g.DrawImage(PicBox.Image, ToRectangle, FromRectangle, GraphicsUnit.Pixel)
My.Computer.Clipboard.SetImage(CurImage)

==================================================================
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
Email:loving-kiss@163.com
优惠接单开发,收费带初学者,组件控件定制开发,成品源代码批发
联系方式:Q64180940(请清楚注明业务还是技术咨询) 全天在线
==================================================================
------解决方案--------------------
参考下这个代码试试:

public partial class Form2 : Form
{
private Rectangle m_Rect;
private Point m_LastMsPoint;
private Point m_LastRectPoint;
private bool m_CanMove;
public Form2()
{
InitializeComponent();

m_Rect = new Rectangle(10, 10, 100, 80);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(SystemPens.HotTrack, this.m_Rect);
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && m_CanMove)
{
int x = m_LastRectPoint.X + e.X - this.m_LastMsPoint.X;
int y = m_LastRectPoint.Y + e.Y - this.m_LastMsPoint.Y;
this.m_Rect.Location = new Point(x, y);
this.panel1.Invalidate();
this.panel2.Invalidate();
}
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
m_LastMsPoint = e.Location;
m_LastRectPoint = m_Rect.Location;
m_CanMove = this.m_Rect.Contains(e.Location);
}

private void panel2_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(0, 0, this.m_Rect.Width, this.m_Rect.Height);
e.Graphics.DrawImage(this.panel1.BackgroundImage, rect, this.m_Rect, GraphicsUnit.Pixel);
}
}