我正在尝试创建一个可调整大小的图像覆盖层(用于裁剪目的)。如果忽略宽高比,似乎很容易调整覆盖层的大小,但我无法弄清如何进行受限制的调整大小并保持宽高比。显然,我不能遵循覆盖层的“手柄”位置(甚至边框),除非强制鼠标跟随它,但这似乎是不自然的,所以我只能依赖鼠标手势(我不介意这样做)。
我也可以轻松调整覆盖层大小,然后强制将其调整为正确的尺寸(就像该网站上关于此主题的所有其他问题一样),但使用鼠标时不太直观。
这就是我想要实现的效果: http://deepliquid.com/projects/Jcrop/demos.php?demo=live_crop 我之前写过一个类似的应用程序,但它是基于浏览器的,所以我使用了一个javascript库。这是一个桌面应用程序,我还没有找到合适的库来完成这个任务。
我已经省略了这段代码中的很多细节,并简化了一些带布尔值的条件。
我也可以轻松调整覆盖层大小,然后强制将其调整为正确的尺寸(就像该网站上关于此主题的所有其他问题一样),但使用鼠标时不太直观。
这就是我想要实现的效果: http://deepliquid.com/projects/Jcrop/demos.php?demo=live_crop 我之前写过一个类似的应用程序,但它是基于浏览器的,所以我使用了一个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