C# 在保持纵横比的同时用鼠标调整长方体的大小?

C# 在保持纵横比的同时用鼠标调整长方体的大小?,c#,.net,winforms,C#,.net,Winforms,我正在尝试创建一个可调整大小的图像覆盖(用于裁剪目的)。如果忽略纵横比,似乎很容易调整覆盖的大小,但我不知道如何根据AR执行受约束的调整大小。我想,除非我强制鼠标跟随,否则我显然无法遵循覆盖的“夹点”位置(甚至边框),但这似乎不自然,所以我只能依靠鼠标手势(我不介意这么做) 我也可以很容易地调整覆盖的大小,然后将其强制到适当的尺寸(就像这个网站上关于这个主题的所有其他问题一样),但使用鼠标时不是很直观 这就是我想要的: 我以前写过这样的应用程序,但它是基于浏览器的,所以我使用了javascri

我正在尝试创建一个可调整大小的图像覆盖(用于裁剪目的)。如果忽略纵横比,似乎很容易调整覆盖的大小,但我不知道如何根据AR执行受约束的调整大小。我想,除非我强制鼠标跟随,否则我显然无法遵循覆盖的“夹点”位置(甚至边框),但这似乎不自然,所以我只能依靠鼠标手势(我不介意这么做)

我也可以很容易地调整覆盖的大小,然后将其强制到适当的尺寸(就像这个网站上关于这个主题的所有其他问题一样),但使用鼠标时不是很直观

这就是我想要的:

我以前写过这样的应用程序,但它是基于浏览器的,所以我使用了javascript库。这是一个桌面应用程序,我还没有找到适合的库

我在这个代码片段中留下了很多细节,并用布尔值简化了一些条件

private void pbImage_Paint(object sender, PaintEventArgs e)
{
    //Overlay
    e.Graphics.FillRectangle(brushRect, overlayRect);

    // Grips
    e.Graphics.FillRectangle(gripRect, leftTopGrip);
    e.Graphics.FillRectangle(gripRect, rightTopGrip);
    e.Graphics.FillRectangle(gripRect, leftBottomGrip);
    e.Graphics.FillRectangle(gripRect, rightBottomGrip);

    AdjustGrips();

    base.OnPaint(e);
}

public void AdjustGrips()
{
    // The next section only causes the grips to partly obey
    // the AR - the rest of the overlay ignores it
    if (overlayRect.Height * arWidth <= overlayRect.Width)
        overlayRect.Width = overlayRect.Height * arWidth;
    else if (overlayRect.Width * arHeight <= overlayRect.Height)
        overlayRect.Height = overlayRect.Width * arHeight;

    leftTopGrip.X = overlayRect.Left;
    leftTopGrip.Y = overlayRect.Top;

    rightTopGrip.X = overlayRect.Right - rightTopGrip.Width;
    rightTopGrip.Y = overlayRect.Top;

    leftBottomGrip.Y = overlayRect.Bottom - leftBottomGrip.Height;
    leftBottomGrip.X = overlayRect.Left;

    rightBottomGrip.X = overlayRect.Right - rightBottomGrip.Width;
    rightBottomGrip.Y = overlayRect.Bottom - rightBottomGrip.Height;

}


private void pbImage_MouseMove(object sender, MouseEventArgs e)
{
    Point pt = new Point(e.X, e.Y);

    // Details elided


    if (e.Button == MouseButtons.Left && mouseinGrip)
    {
        if (bottomRightIsGripped)
        {
            newOverlayRect.X = overlayRect.X;
            newOverlayRect.Y = overlayRect.Y;
            newOverlayRect.Width = pt.X - newOverlayRect.Left;
            newOverlayRect.Height = pt.Y - newOverlayRect.Top;

            if (newOverlayRect.X > newOverlayRect.Right)
            {
                newOverlayRect.Offset(-width, 0);
                if (newOverlayRect.X < 0)
                    newOverlayRect.X = 0;
            }

            if (newOverlayRect.Y > newOverlayRect.Bottom)
            {
                newOverlayRect.Offset(0, -height);
                if (newOverlayRect.Y < 0)
                    newOverlayRect.Y = 0;
            }

            pbImage.Invalidate();
            oldOverlayRect = overlayRect = newOverlayRect;
            Cursor = Cursors.SizeNWSE;
        }

        // Code for other grips elided
    }   

    AdjustGrips();
    pbImage.Update();
    base.OnMouseMove(e);
}

// Mouse up and down elided
private void pbImage\u Paint(对象发送方,PaintEventArgs e)
{
//覆盖层
e、 图形.圆角矩形(brushRect,OverlyRect);
//握紧
e、 图形.圆角矩形(gripRect,leftTopGrip);
e、 图形.圆角矩形(gripRect,rightTopGrip);
e、 图形.圆角矩形(gripRect,leftBottomGrip);
e、 图形.圆角矩形(gripRect,rightBottomGrip);
调整夹点();
基础漆(e);
}
公共图书馆
{
//下一节仅使夹点部分服从
//AR-覆盖的其余部分忽略它
if(覆盖高度*ARWITH新覆盖高度底部)
{
新的重叠长度偏移(0,-高度);
if(newOverlyRect.Y<0)
newOverlayRect.Y=0;
}
pbImage.Invalidate();
oldOverlayRect=overlayRect=newOverlayRect;
Cursor=Cursors.SizeNWSE;
}
//省略其他夹点的代码
}   
调整夹点();
pbImage.Update();
基地移动(e);
}
//鼠标上下消去

您可以完全控制覆盖层拖动时的新大小

您给出的示例链接只是基于单击选择一个起点,然后选择
Max(Abs(pt.x-start.x)、Abs(pt.y-start.y))
,并基于该起点选择裁剪平方

要使用非平方比,请首先规格化距离

// given known data 
// 
// Point start; 
// The starting location of the mouse down for the drag, 
// or the top left / bottom right of the crop based on if the mouse is 
// left/above the starting point
// 
// Size ratio;
// The ratio of the result crop
//

// pt = (20)x(-20)
// start = (0),(0)
// ratio = (1)x(2)
var dist = new Point(pt.X - start.X, pt.Y - start.Y);

// "normalize" the vector from the ratio
// normalized vector is the distances with respect to the ratio
// ratio is (1)x(2). A (20)x(-20) is normalized as (20),(-10)
var normalized = new Point(dist.X / ratio.Width, dist.Y / ratio.Height);

// In our (20),(-10) example, we choose the ratio's height 20 as the larger normal.
// we will base our new size on the height
var largestNormal = (Math.Abs(normalized.X) > Math.Abs(normalized.Y)
                        ? Math.Abs(normalized.X) : Math.Abs(normalized.Y);

// The calcedX will be 20, calcedY will be 40
var calcedOffset = (largestNormal * ratio.Width, largestNormal * ratio.Height);

// reflect the calculation back to the correct quarter
// final size is (20)x(-40)
if (distX < 0) calcedOffset.X *= -1;
if (distY < 0) calcedOffset.Y *= -1;

var newPt = new Point(start.X + calcedOffset.X, start.Y + calcedOffset.Y);
//给定已知数据
// 
//起点;
//鼠标向下拖动的起始位置,
//或裁剪的左上/右下角,取决于鼠标是否移动
//左/高于起点
// 
//尺寸比;
//结果作物的比例
//
//pt=(20)x(-20)
//开始=(0),(0)
//比率=(1)x(2)
var dist=新点(pt.X-start.X,pt.Y-start.Y);
//从比率“规格化”向量
//归一化向量是相对于比率的距离
//比率为(1)x(2)。A(20)x(-20)被归一化为(20),(-10)
var归一化=新点(距离X/比率宽度、距离Y/比率高度);
//在我们的(20),(-10)示例中,我们选择比率的高度20作为较大的法线。
//我们将根据身高来确定新尺码
var largestNormal=(Math.Abs(normalized.X)>Math.Abs(normalized.Y)
?Math.Abs(normalized.X):Math.Abs(normalized.Y);
//calcedX将是20,calcedY将是40
var calcedOffset=(最大法线*比率.宽度,最大法线*比率.高度);
//将计算结果反映回正确的季度
//最终尺寸为(20)x(-40)
如果(distX<0)calcedOffset.X*=-1;
如果(距离<0)calcedOffset.Y*=-1;
var newPt=新点(start.X+calcedOffset.X,start.Y+calcedOffset.Y);

请注意,其中一个长度可以大于鼠标位置,但它永远不会小于鼠标位置。这将产生鼠标沿新裁剪框边缘移动的效果,并保持框的比例。

我已经找出了代码中原始问题的原因。与静态图像调整大小不同,宽高比代码dep结束您正在“保持”的夹点,所以在所有情况下(例如,当设置夹点位置时)将其放在公共位置将不起作用。您可以轻松地计算下一次更新时rect的大小,但位置应根据保持的夹点进行设置

例如,如果通过按住左上夹点来调整大小,则裁剪矩形的底部和右侧应保持静止。如果保持代码不变,则矩形可以正确调整大小,但它会在画布上移动和/或夹点与矩形的角点不同步。可能有更好的方法这样做,但这里有一些粗糙的代码可以工作。我只包含了右下角和左上角夹点的代码来说明差异。忽略了设置鼠标指针和错误检查等无关的事情

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    Point mousePosition = new Point(e.X, e.Y);

    if (e.Button == MouseButtons.Left)
    {
        // This resizeMode, moveMode and other booleans
        // are set in the MouseUp event

        if (resizeBottomLeft)
        {
            // Top and Right should remain static!
            newCropRect.X = mousePosition.X;
            newCropRect.Y = currentCropRect.Y;
            newCropRect.Width = currentCropRect.Right - mousePosition.X;
            newCropRect.Height = mousePosition.Y - newCropRect.Top;

            if (newCropRect.X > newCropRect.Right)
            {
                newCropRect.Offset(cropBoxWidth, 0);
                if (newCropRect.Right > ClientRectangle.Width)
                    newCropRect.Width = ClientRectangle.Width - newCropRect.X;
            }

            if (newCropRect.Y > newCropRect.Bottom)
            {
                newCropRect.Offset(0, -cropBoxHeight);
                if (newCropRect.Y < 0)
                    newCropRect.Y = 0;
            }

            // Aspect Ratio + Positioning
            if (newCropRect.Width > newCropRect.Height)
            {
                newCropRect.Height = (int)(newCropRect.Width / ASPECT_RATIO);
            }
            else
            {
                int newWidth = (int)(newCropRect.Height * ASPECT_RATIO);
                newCropRect.X = newCropRect.Right - newWidth;
                newCropRect.Width = newWidth;
            }
        }
        else if (resizeTopRight)
        {
            // Bottom and Left should remain static!
            newCropRect.X = oldCropRect.X;
            newCropRect.Y = mousePosition.Y;
            newCropRect.Width = mousePosition.X - newCropRect.Left;
            newCropRect.Height = oldCropRect.Bottom - mousePosition.Y;

            if (newCropRect.X > newCropRect.Right)
            {
                newCropRect.Offset(-cropBoxWidth, 0);
                if (newCropRect.X < 0)
                    newCropRect.X = 0;
            }
            if (newCropRect.Y > newCropRect.Bottom)
            {
                newCropRect.Offset(0, cropBoxHeight);
                if (newCropRect.Bottom > ClientRectangle.Height)
                    newCropRect.Y = ClientRectangle.Height - newCropRect.Height;
            }

            // Aspect Ratio + Positioning
            if (newCropRect.Width > newCropRect.Height)
            {
                int newHeight = (int)(newCropRect.Width / ASPECT_RATIO);
                newCropRect.Y = newCropRect.Bottom - newHeight;
                newCropRect.Height = newHeight;
            }
            else
            {
                int newWidth = (int)(newCropRect.Height * ASPECT_RATIO);
                newCropRect.Width = newWidth;
            }
        }
        else if (moveMode) //Moving the rectangle
        {
            newMousePosition = mousePosition;
            int dx = newMousePosition.X - oldMousePosition.X;
            int dy = newMousePosition.Y - oldMousePosition.Y;
            currentCropRect.Offset(dx, dy);
            newCropRect = currentCropRect;
            oldMousePosition = newMousePosition;
        }

        if (resizeMode || moveMode)
        {
            oldCropRect = currentCropRect = newCropRect;

            // Set the new position of the grips
            AdjustGrips();
            pictureBox1.Invalidate();
            pictureBox1.Update();
        }
    }
}
private void pictureBox1\u MouseMove(对象发送方,MouseEventArgs e)
{
点鼠标位置=新点(e.X,e.Y);
if(e.Button==MouseButtons.Left)
{
//此选项包括resizeMode、moveMode和其他布尔值
//在MouseUp事件中设置
如果(resizeBottomLeft)
{
//顶部和右侧应保持静止!
newCropRect.X=mousePosition.X;
newCropRect.Y=当前croprect.Y;
newCropRect.Width=currentCropRect.Right-mousePosition.X;
newCropRect.Height=鼠标位置.Y-newCropRect.Top;
如果(newCropRect.X>newCropRect.Right)
{
newCropRect.Offset(cropBoxWidth,0);
如果(newCropRect.Right>ClientRectangle.Width)
newCropRect.Width=ClientRectangle.Width-newCropRect.X;
}
如果(newCropRect.Y>newCropRect.Bottom)
{
newCropRect.Offset(0,-cropBoxHeight);
如果(newCropRec