サイズ変更可能な画像オーバーレイを作成しようとしています (トリミング目的で)。アスペクト比を無視すれば、オーバーレイのサイズを変更するのはかなり簡単に思えますが、AR を考慮して制限付きのサイズ変更を実行する方法がわかりません。マウスを強制的に追従させない限り、オーバーレイの「グリップ」位置(または境界線)に従うことは明らかにできないと思いますが、それは不自然に思えるので、マウスジェスチャに頼る必要があります(これは私はしません)気にしないでください)。
また、オーバーレイのサイズを簡単に変更し、後で適切なサイズに強制することもできます (このサイトのこのトピックに関する他のすべての質問がそうであるように) が、マウスを使用する場合はあまり直感的ではありません。
これは私が目指しているものです: 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