![](/img/trans.png)
[英]Getting correct coordinates from scaled picture box with Zoom SizeMode
[英]Draw Rectangle inside picture box SizeMode Zoom
我在 WindowsForms 項目中有一個圖片框,其 SizeMode 為“縮放”。
我想在圖像內繪制一個矩形並獲取其相對於圖像而不是圖片框的坐標。
問題是矩形的坐標與在 Windows Paint Application 上選擇的相同矩形不匹配。
這是使用的代碼:
開始繪畫:
/// <summary> /// Starts drawing. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { backupImage = pictureBox1.Image; _once = true; RectStartPoint = e.Location; pictureBox1.Invalidate(); }
移動鼠標時:
/// <summary> /// While moving mouse event, paint rectangle /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (_once) //Only draw rectangle while drawing mode { Point tempEndPoint = e.Location; Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X), Math.Min(RectStartPoint.Y, tempEndPoint.Y)); Rect = new Rectangle( Math.Min(tempEndPoint.X, Rect.Left), Math.Min(tempEndPoint.Y, Rect.Top), Math.Min(eX - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X), Math.Min(eY - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y)); pictureBox1.Refresh(); pictureBox1.CreateGraphics().DrawRectangle(cropPen, Rect); } }
2 單擊時,完成繪制矩形:
/// <summary> /// When mouse click is released, write in texbox the rectangle's coordinates. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (_once) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { Point tempEndPoint = e.Location; _once = false; string sAux = string.Format("Left: {0}; Top: {1}; Width: {2}; Height: {3} \\r\\n", Math.Min(tempEndPoint.X, Rect.Left), Math.Min(tempEndPoint.Y, Rect.Top), Math.Min(eX - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X), Math.Min(eY - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y)); textBox1.Text += sAux; } } }
結果是:
正如您在兩張圖片中看到的,左、上、寬和高不匹配。
你能告訴我如何獲得相同的結果嗎?
這是一個幫助各種計算的函數:
void SetImageScale(PictureBox pbox, out RectangleF ImgArea, out float zoom)
{
SizeF sp = pbox.ClientSize;
SizeF si = pbox.Image.Size;
float rp = sp.Width / sp.Height; // calculate the ratios of
float ri = si.Width / si.Height; // pbox and image
if (rp > ri)
{
zoom = 1f * sp.Height / si.Height;
float width = si.Width * zoom;
float left = (sp.Width - width) / 2;
ImgArea = new RectangleF(left, 0, width, sp.Height);
}
else
{
zoom = 1f * sp.Width / si.Width;
float height = si.Height * zoom;
float top = (sp.Height - height) / 2;
ImgArea = new RectangleF(0, top, sp.Width, height);
}
}
給定一個根據鼠標坐標創建的Rectangle Rect
,您可以如何使用它:
float zoom = 1f;
RectangleF ImgArea = Rectangle.Empty;
SetImageScale(pictureBox1, out ImgArea, out zoom);
Point RLoc = Point.Round(new PointF( (Rect.X - ImgArea.X) / zoom,
(Rect.Y - ImgArea.Y) / zoom ));
Size RSz = Size.Round(new SizeF(Rect.Width / zoom, Rect.Height / zoom));
label1.Text = "Selection in mouse coordinates: " + Rect.ToString();
label2.Text = "Selection in image coordinates: " + new Rectangle(RLoc, RSz).ToString();
無論圖像是橫向還是縱向,或者哪個比例(如果有的話)更大,圖像或圖片框,這都應該有效。
請注意,在圖像強烈縮放的情況下,很難進行像素完美選擇。
該功能是在一個變種這個職位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.