在缩放模式 Picturebox 中平移矩形位置导致负 Y 坐标 [英] Translating Rectangle Position in Zoom Mode Picturebox Results in Negative Y Coordinate

查看:43
本文介绍了在缩放模式 Picturebox 中平移矩形位置导致负 Y 坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下类参考 - 在缩放模式下平移矩形位置图片框将位图中的矩形选择转换为在 UI 中的图片框上绘制的帧控件.

I'm using the following class Ref - Translate Rectangle Position in Zoom Mode Picturebox to translate a rectangle selection within a Bitmap to a Frame Control drawn over a picturebox in the UI.

public class ZoomFactor
{
    public ZoomFactor() { }

    public PointF TranslateZoomPosition(PointF coordinates, SizeF containerSize, SizeF imageSize)
    {
        PointF imageOrigin = TranslateCoordinatesOrigin(coordinates, containerSize, imageSize);
        float scaleFactor = GetScaleFactor(containerSize, imageSize);
        return new PointF(imageOrigin.X / scaleFactor, imageOrigin.Y / scaleFactor);
    }

    public RectangleF TranslateZoomSelection(RectangleF selectionRect, SizeF containerSize, SizeF imageSize)
    {
        PointF selectionTrueOrigin = TranslateZoomPosition(selectionRect.Location, containerSize, imageSize);
        float scaleFactor = GetScaleFactor(containerSize, imageSize);

        SizeF selectionTrueSize = new SizeF(selectionRect.Width / scaleFactor, selectionRect.Height / scaleFactor);
        return new RectangleF(selectionTrueOrigin, selectionTrueSize);
    }

    public RectangleF TranslateSelectionToZoomedSel(RectangleF selectionRect, SizeF containerSize, SizeF imageSize)
    {
        float scaleFactor = GetScaleFactor(containerSize, imageSize);
        RectangleF zoomedSelectionRect = new
            RectangleF(selectionRect.X * scaleFactor, selectionRect.Y * scaleFactor,
                       selectionRect.Width * scaleFactor, selectionRect.Height * scaleFactor);

        PointF imageScaledOrigin = GetImageScaledOrigin(containerSize, imageSize);
        zoomedSelectionRect.Location = new PointF(zoomedSelectionRect.Location.X + imageScaledOrigin.X,
                                                  zoomedSelectionRect.Location.Y + imageScaledOrigin.Y);
        return zoomedSelectionRect;
    }

    public PointF TranslateCoordinatesOrigin(PointF coordinates, SizeF containerSize, SizeF imageSize)
    {
        PointF imageOrigin = GetImageScaledOrigin(containerSize, imageSize);
        return new PointF(coordinates.X - imageOrigin.X, coordinates.Y - imageOrigin.Y);
    }

    public PointF GetImageScaledOrigin(SizeF containerSize, SizeF imageSize)
    {
        SizeF imageScaleSize = GetImageScaledSize(containerSize, imageSize);
        return new PointF((containerSize.Width - imageScaleSize.Width) / 2,
                          (containerSize.Height - imageScaleSize.Height) / 2);
    }

    public SizeF GetImageScaledSize(SizeF containerSize, SizeF imageSize)
    {
        float scaleFactor = GetScaleFactor(containerSize, imageSize);
        return new SizeF(imageSize.Width * scaleFactor, imageSize.Height * scaleFactor);

    }
    internal float GetScaleFactor(SizeF scaled, SizeF original)
    {
        return (original.Width > original.Height) ? (scaled.Width / original.Width)
                                                  : (scaled.Height / original.Height);
    }
}

// Using the Class
RectangleF BitmapRect = ZoomHelper.TranslateSelectionToZoomedSel(ResizedRect, new SizeF(pbox_preview.Width, pbox_preview.Height), bit.Size);

这适用于大多数输入,但当提供以下输入时,返回的 Y 坐标为负且缩放错误.

This works fine for most inputs but when the following input is provided the returned Y coordinate is negative and the scaling is wrong.

ResizedRect --> {X = 37 Y = 2 Width = 227 Height = 308} 

PictureBoxSize ---> SizeF(603,423)

imagesize --->  (311,310)

The returned rectangle is this 
{X = 71.73955 Y = -85.15273 Width = 440.131836 Height = 597.1833}

Y 坐标为负.

推荐答案

你知道:

  • 如何计算缩放图片框的缩放系数(scale).
  • 如何获取缩放图片框中的图像偏移量(左上角).

然后在图像上有一个矩形(原始大小),这就是如何计算缩放图片框上的矩形大小和位置:

Then having a rectangle on the image (with original size), this is how you can calculate the rectangle size and position on the zoomed picture box:

  1. 使用垂直和水平比例缩小矩形
  2. 使用缩放图片框的图像偏移来偏移结果.

这里有两种方法:

  • GetRectangeOnImage:在放大的图片框上有矩形,它返回原始图像上的矩形
  • GetRectangeOnPictureBox:具有原始图像上的矩形,它返回缩放后的图片框上的矩形.

代码:

public RectangleF GetRectangeOnImage(PictureBox p, Rectangle rectOnPictureBox)
{
    var method = typeof(PictureBox).GetMethod("ImageRectangleFromSizeMode",
        System.Reflection.BindingFlags.NonPublic | 
        System.Reflection.BindingFlags.Instance);
    var imageRect = (Rectangle)method.Invoke(p, new object[] { p.SizeMode });
    if (p.Image == null)
        return rectOnPictureBox;
    var cx = (float)p.Image.Width / (float)imageRect.Width;
    var cy = (float)p.Image.Height / (float)imageRect.Height;
    rectOnPictureBox.Offset(-imageRect.X, -imageRect.Y);
    return new RectangleF(rectOnPictureBox.X * cx, rectOnPictureBox.Y * cy,
        rectOnPictureBox.Width * cx, rectOnPictureBox.Height * cy);
}

public RectangleF GetRectangeOnPictureBox(PictureBox p, Rectangle rectOnImage)
{
    var method = typeof(PictureBox).GetMethod("ImageRectangleFromSizeMode",
        System.Reflection.BindingFlags.NonPublic | 
        System.Reflection.BindingFlags.Instance);
    var imageRect = (Rectangle)method.Invoke(p, new object[] { p.SizeMode });
    if (p.Image == null)
        return rectOnImage;
    var cx = (float)p.Image.Width / (float)imageRect.Width;
    var cy = (float)p.Image.Height / (float)imageRect.Height;
    var r2 = new RectangleF(rectOnImage.X / cx, rectOnImage.Y / cy,
        rectOnImage.Width / cx, rectOnImage.Height / cy);
    r2.Offset(imageRect.X, imageRect.Y);
    return r2;
}

这篇关于在缩放模式 Picturebox 中平移矩形位置导致负 Y 坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆