根据 SizeMode 转换 PictureBox 选区矩形
Convert PictureBox Selection Rectangle according to SizeMode
我有两个 PictureBoxes
pbOriginal 和 pbFace。在从 pbOriginal 中的图像中选择 "face" 之后,我克隆了矩形选区并将其放入 pbFace 中。
但是,因为 pbOriginal 使用 SelectionMode=Stretch
实际复制的区域与选择的区域不同。
如何转换矩形的坐标,使其真正反映拉伸图像的坐标?
这是一个在绘制第一个矩形的同时绘制第二个矩形的示例..:[=17=]
Point mDown = Point.Empty;
Point mCurr = Point.Empty;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{ mDown = e.Location; }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ mCurr = e.Location; pictureBox1.Invalidate(); }
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = new Rectangle(mDown.X, mDown.Y, mCurr.X - mDown.X, mCurr.Y - mDown.Y);
e.Graphics.DrawRectangle(Pens.Orange, r);
pictureBox2.Invalidate();
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
if (pictureBox2.Image == null) return;
float stretch1X = 1f * pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float stretch1Y = 1f * pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
int x = (int)(mDown.X * stretch1X);
int y = (int)(mDown.Y * stretch1Y);
int x2 = (int)(mCurr.X * stretch1X);
int y2 = (int)(mCurr.Y * stretch1Y);
Rectangle r = new Rectangle(x, y, x2 - x, y2 - y);
e.Graphics.DrawRectangle(Pens.Orange, r);
}
请注意,它假定您正在从左上角到右下角绘制..
如果您想复制选择,您可以使用相同的因素和相同的 Rectangle
作为 DrawImage
调用的来源:
float stretch1X = 1f * pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float stretch1Y = 1f * pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
Point pt = new Point((int)(mDown.X * stretch1X), (int)(mDown.Y * stretch1Y));
Size sz = new Size((int)((mCurr.X - mDown.X) * stretch1X),
(int)((mCurr.Y - mDown.Y) * stretch1Y));
Rectangle rSrc = new Rectangle(pt, sz);
Rectangle rDest= new Rectangle(Point.Empty, sz);
Bitmap bmp = new Bitmap(sz.Width, sz.Height);
using (Graphics G = Graphics.FromImage(bmp))
G.DrawImage(pictureBox1.Image, rDest, rSrc , GraphicsUnit.Pixel);
pictureBox2.Image = bmp;
您可能希望对 MouseUp
事件进行编码以存储最终 mCurr
位置并触发复制..
我有两个 PictureBoxes
pbOriginal 和 pbFace。在从 pbOriginal 中的图像中选择 "face" 之后,我克隆了矩形选区并将其放入 pbFace 中。
但是,因为 pbOriginal 使用 SelectionMode=Stretch
实际复制的区域与选择的区域不同。
如何转换矩形的坐标,使其真正反映拉伸图像的坐标?
这是一个在绘制第一个矩形的同时绘制第二个矩形的示例..:[=17=]
Point mDown = Point.Empty;
Point mCurr = Point.Empty;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{ mDown = e.Location; }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ mCurr = e.Location; pictureBox1.Invalidate(); }
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle r = new Rectangle(mDown.X, mDown.Y, mCurr.X - mDown.X, mCurr.Y - mDown.Y);
e.Graphics.DrawRectangle(Pens.Orange, r);
pictureBox2.Invalidate();
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
if (pictureBox2.Image == null) return;
float stretch1X = 1f * pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float stretch1Y = 1f * pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
int x = (int)(mDown.X * stretch1X);
int y = (int)(mDown.Y * stretch1Y);
int x2 = (int)(mCurr.X * stretch1X);
int y2 = (int)(mCurr.Y * stretch1Y);
Rectangle r = new Rectangle(x, y, x2 - x, y2 - y);
e.Graphics.DrawRectangle(Pens.Orange, r);
}
请注意,它假定您正在从左上角到右下角绘制..
如果您想复制选择,您可以使用相同的因素和相同的 Rectangle
作为 DrawImage
调用的来源:
float stretch1X = 1f * pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float stretch1Y = 1f * pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
Point pt = new Point((int)(mDown.X * stretch1X), (int)(mDown.Y * stretch1Y));
Size sz = new Size((int)((mCurr.X - mDown.X) * stretch1X),
(int)((mCurr.Y - mDown.Y) * stretch1Y));
Rectangle rSrc = new Rectangle(pt, sz);
Rectangle rDest= new Rectangle(Point.Empty, sz);
Bitmap bmp = new Bitmap(sz.Width, sz.Height);
using (Graphics G = Graphics.FromImage(bmp))
G.DrawImage(pictureBox1.Image, rDest, rSrc , GraphicsUnit.Pixel);
pictureBox2.Image = bmp;
您可能希望对 MouseUp
事件进行编码以存储最终 mCurr
位置并触发复制..