放大面板中的图像中心

时间:2013-05-09 06:23:44

标签: c# winforms

我正在尝试制作一个允许在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方法中switch语句中有空OnMouseMove块的原因。

0 个答案:

没有答案