0

我正在尝试制作一个面板,允许在 winforms 和 .Net 3.5 中缩放和平移图像我想我快到了,但是在缩放功能上遇到了麻烦。

我试图放大图像的中心,但不知何故有点偏离。我在考虑几何形状时遇到了非常糟糕的时间,并且非常感谢一些帮助来找出我哪里出错了。

这是代码:

public class ImageViewer : Panel
{
    public double ZoomFactor
    {
        get { return this.zoomFactor; }
        set
        {
            this.zoomFactor = value;
            this.Invalidate();
        }
    }

    public Point Translation { get; set; }

    /// <summary>
    /// The image to be displayed
    /// </summary>
    [Category("Appearance"), Description("The image to be displayed.")]
    public Image Image
    {
        get { return this.image; }
        set
        {
            this.image = value;
            this.ZoomExtents();
            this.Invalidate();
        }
    }

    private double initialScale;
    private double zoomFactor;
    private Image image;
    private Point startingPoint;

    /// <summary>
    /// Class constructor
    /// </summary>
    public ImageViewer()
    {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }

    /// <summary>
    /// Sets the Zoom to a value where the whole image is visible.
    /// </summary>
    public void ZoomExtents()
    {
        if (this.Image == null)
            return;

        this.initialScale = Math.Min((double)this.Width / this.Image.Width, (double)this.Height / this.Image.Height);

        this.Invalidate();
    }

    /// <summary>
    /// Mouse down event handler
    /// </summary>
    protected override void OnMouseDown(MouseEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButtons.Middle:
                startingPoint = new Point(e.Location.X - this.Translation.X, e.Location.Y - this.Translation.Y);
                break;
        }

        base.OnMouseDown(e);
    }

    /// <summary>
    /// Mouse move event handler
    /// </summary>
    protected override void OnMouseMove(MouseEventArgs e)
    {
        Point mousePosition = Control.MousePosition;

        switch (e.Button)
        {
            case MouseButtons.Middle:
                if (this.Bounds.Contains(e.Location))
                    Translation = new Point(e.Location.X - startingPoint.X, e.Location.Y - startingPoint.Y);
                this.Invalidate();
                break;

            case MouseButtons.Right:
                // Add functionality later
                break;
        }

        base.OnMouseMove(e);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        float scale;
        float[] translation;

        if (this.Image == null)
            return;

        scale = (float)(this.initialScale * this.ZoomFactor);
        translation = new float[] { (float)(this.Translation.X / scale), ((float)this.Translation.Y / scale) };

        e.Graphics.Clear(this.BackColor);

        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.TranslateTransform(translation[0], translation[1]);

        e.Graphics.DrawImage(this.image, new Rectangle(new Point((int)(-this.Width / 2 * scale), (int)(-this.Height / 2 * scale)), new Size(this.image.Width, this.image.Height)));

        base.OnPaint(e);
    }
}

通过按下滚轮激活平移。缩放是通过设置zoomFactor变量来完成的。最终我想通过鼠标右键来完成,这就是为什么方法中的语句中有空case块的原因。switchOnMouseMove

4

0 回答 0