【发布时间】:2013-09-17 01:22:43
【问题描述】:
我正在尝试创建一个可调整大小的图像叠加层(用于裁剪目的)。如果我忽略纵横比,调整叠加层的大小似乎很容易,但我不知道如何执行尊重 AR 的约束调整大小。我认为除非我强迫鼠标跟随它,否则我显然不能服从覆盖的“抓地力”位置(甚至边界),但这似乎不自然,所以我只需要依靠鼠标手势(我不这样做'不介意这样做)。
我还可以轻松调整叠加层的大小,然后将其强制调整为适当的尺寸(就像本网站上有关此主题的所有其他问题一样),但使用鼠标时不是很直观。
这就是我想要的: http://deepliquid.com/projects/Jcrop/demos.php?demo=live_crop
我以前写过这样的应用程序,但它是基于浏览器的,所以我使用了一个 javascript 库。这是一个桌面应用程序,我还没有找到合适的库。
我在这段代码 sn-p 中留下了很多细节,并用布尔值简化了一些条件。
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
【问题讨论】:
-
您的问题是什么?您是否正在寻找可以执行此操作的库?如果是这样,那就离题了。
-
不,我正在寻找执行此操作所涉及的技术。我在使用 javascript 库之前已经这样做了,但我找不到桌面库,这是轶事,只是为了表明 StackOverflow 不是我的第一站。我不太确定反对票是关于什么的。我应该在不遵守 AR 的情况下发布处理调整大小的代码吗?
-
是的,很多 SO 用户在看不到任何代码时会感到难过。 (我没有DV,觉得这里不合适)但是,展示你尝试过的东西,然后问如何用它来维护AR。
-
我已经添加了一些我正在处理的代码。
-
这就是为什么好的 CS 学位涉及大量数学的原因。