3

サイズ変更可能な画像オーバーレイを作成しようとしています (トリミング目的で)。アスペクト比を無視すれば、オーバーレイのサイズを変更するのはかなり簡単に思えますが、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
4

2 に答える 2

2

コード内の元の問題の原因を突き止めました。静的な画像のサイズ変更とは異なり、縦横比コードは「保持」しているグリップに依存するため、すべての場合に共通の場所 (たとえば、グリップ位置が設定されている場合) に配置すると機能しません。次の更新時に四角形のサイズを簡単に計算できますが、保持されているグリップに応じて位置を設定する必要があります。

たとえば、左上のグリップを保持してサイズを変更している場合、トリミング四角形の下辺と右辺は静止したままにする必要があります。コードを同じままにしておくと、長方形のサイズは正しく変更されますが、キャンバスの周りを移動したり、グリップが長方形の角と同期しなくなったりします。おそらくこれを行うためのより良い方法がありますが、動作する粗いコードを次に示します。違いを説明するために、右下と左上のグリップのコードのみを含めました。マウスポインタの設定やエラーチェックなど余計なことは省略。

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();
        }
    }
}
于 2013-09-16T17:09:44.163 に答える