缩放图像中心

时间:2012-08-05 07:44:25

标签: c# winforms image-zoom

我创建了一个放大/缩小图像的程序。

但它不会放大到中心。

我的图片框位于面板中,代码为:

Image newImage = Image.FromFile("view.jpg");
pictureBox1.Width = (int)(newImage.Size.Width / ZoomLevel);
pictureBox1.Height = (int)(newImage.Size.Height / ZoomLevel);

Bitmap img = new Bitmap(newImage, 
                        (int)(newImage.Size.Width / ZoomLevel), 
                        (int)(newImage.Size.Height / ZoomLevel));
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.Image = img;

I uploaded a sample project here. plz download this

我使用Visual Studio 2008,C#,。Net 3.5

提前感谢。

3 个答案:

答案 0 :(得分:2)

我找到了答案。

我上传here(链接不再有效)

它是一个拖放图像的示例项目。 :)

答案 1 :(得分:2)

Reza的解决方案非常有效,但由于需要注册,因此有点难以访问。我已将其代码重写为独立控件,并将代码放在此处以供将来参考。

public class ImageViewer : Panel
{
    public Image Image
    {
        get { return this.image; }
        set
        {
            this.image = value;
            this.ZoomExtents();
            this.LimitBasePoint(basePoint.X, basePoint.Y);
            this.Invalidate();
        }
    }

    private bool drag;
    private float ScaleFactor = 1;
    private Point basePoint;
    private Image image;
    private int x, y;

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

    /// <summary>
    /// Mouse button down event
    /// </summary>
    protected override void OnMouseDown(MouseEventArgs e)
    {
        switch (e.Button)
        {
            case MouseButtons.Left:
                this.ZoomIn();
                break;

            case MouseButtons.Middle:
                this.drag = true;
                x = e.X;
                y = e.Y;
                break;

            case MouseButtons.Right:
                this.ZoomOut();
                break;
        }

        base.OnMouseDown(e);
    }

    /// <summary>
    /// Mouse button up event
    /// </summary>
    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Middle)
            drag = false;

        base.OnMouseUp(e);
    }

    /// <summary>
    /// Mouse move event
    /// </summary>
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (drag)
        {
            LimitBasePoint(basePoint.X + e.X - x, basePoint.Y + e.Y - y);
            x = e.X;
            y = e.Y;
            this.Invalidate();
        }

        base.OnMouseMove(e);
    }

    /// <summary>
    /// Resize event
    /// </summary>
    protected override void OnResize(EventArgs e)
    {
        LimitBasePoint(basePoint.X, basePoint.Y);
        this.Invalidate();

        base.OnResize(e);
    }

    /// <summary>
    /// Paint event
    /// </summary>
    protected override void OnPaint(PaintEventArgs pe)
    {
        if (this.Image != null)
        {
            Rectangle src = new Rectangle(0, 0, Image.Width, Image.Height);
            Rectangle dst = new Rectangle(basePoint.X, basePoint.Y, (int)(Image.Width * ScaleFactor), (int)(Image.Height * ScaleFactor));
            pe.Graphics.DrawImage(Image, dst, src, GraphicsUnit.Pixel);
        }

        base.OnPaint(pe);
    }

    private void ZoomExtents()
    {
        if (this.Image != null)
            this.ScaleFactor = (float)Math.Min((double)this.Width / this.Image.Width, (double)this.Height / this.Image.Height);
    }

    private void ZoomIn()
    {
        if (ScaleFactor < 10)
        {
            int x = (int)((this.Width / 2 - basePoint.X) / ScaleFactor);
            int y = (int)((this.Height / 2 - basePoint.Y) / ScaleFactor);
            ScaleFactor *= 2;
            LimitBasePoint((int)(this.Width / 2 - x * ScaleFactor), (int)(this.Height / 2 - y * ScaleFactor));
            this.Invalidate();
        }
    }

    private void ZoomOut()
    {
        if (ScaleFactor > .1)
        {
            int x = (int)((this.Width / 2 - basePoint.X) / ScaleFactor);
            int y = (int)((this.Height / 2 - basePoint.Y) / ScaleFactor);
            ScaleFactor /= 2;
            LimitBasePoint((int)(this.Width / 2 - x * ScaleFactor), (int)(this.Height / 2 - y * ScaleFactor));
            this.Invalidate();
        }
    }

    private void LimitBasePoint(int x, int y)
    {
        if (this.Image == null)
            return;

        int width = this.Width - (int)(Image.Width * ScaleFactor);
        int height = this.Height - (int)(Image.Height * ScaleFactor);
        if (width < 0)
        {
            x = Math.Max(Math.Min(x, 0), width);
        }
        else
        {
            x = width / 2;
        }
        if (height < 0)
        {
            y = Math.Max(Math.Min(y, 0), height);
        }
        else
        {
            y = height / 2;
        }
        basePoint = new Point(x, y);
    }
}

显然,所有的功劳归于Reza。

答案 2 :(得分:0)

我认为这是因为你改变了图片框的大小。 它居中但我看不到它,因为你总能看到所有图片。

尝试删除2行,更改图片框的宽度和高度,或将它们设置为常量,如1或2或...如果您想要根据图片:

第一个选项:

Image newImage = Image.FromFile("view.jpg");
Bitmap img = new Bitmap(newImage, 
                        (int)(newImage.Size.Width / ZoomLevel), 
                        (int)(newImage.Size.Height / ZoomLevel));
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.Image = img;

第二个选项:

Image newImage = Image.FromFile("view.jpg");
pictureBox1.Width = (int)(newImage.Size.Width / 1);
pictureBox1.Height = (int)(newImage.Size.Height / 1);

Bitmap img = new Bitmap(newImage, 
                        (int)(newImage.Size.Width / ZoomLevel), 
                        (int)(newImage.Size.Height / ZoomLevel));
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.Image = img;