缩放和滚动图像

问题描述 投票:0回答:1

在我的绘画事件中,我试图让图像居中,但也允许用户通过滚动来放大照片。当滚轮出现时,图像似乎偏离了一点,并在屏幕上绘制并切断了任何可以提供帮助的图像?

protected override void OnPaint(PaintEventArgs e)

        {
           if (_image == null)

           {
               base.OnPaintBackground(e);
               return;
           }
            Rectangle rect = this.DisplayRectangle ;

            // Get a gradient brush and fill the background. 
            Brush backBrush =  new SolidBrush( Color.Gray);
            e.Graphics.FillRectangle(backBrush, rect);
            int x = (this.Width - (int)(_image.Width * _zoom)) / 2;
            int y = (this.Height - (int)(_image.Height * _zoom)) / 2;
            // Calculate the panning amount to keep the image centered
            Matrix mx = new Matrix(_zoom, 0, 0, _zoom, x, y);
            mx.Translate(this.AutoScrollPosition.X / _zoom - x, this.AutoScrollPosition.Y / _zoom- y);
            e.Graphics.Transform = mx;
            e.Graphics.InterpolationMode = _interpolationMode;
            // Calculate the top-left corner of the image
            
            e.Graphics.DrawImage(_image, new Rectangle(x, y, this._image.Width, this._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);

            base.OnPaint(e);
        }
c# winforms zooming scrollable
1个回答
0
投票

我修复了它`Rectangle rect = this.DisplayRectangle ;

        // Get a gradient brush and fill the background. 
        Brush backBrush =  new SolidBrush( Color.Gray);
        e.Graphics.FillRectangle(backBrush, rect);
       
        int x = (this.Width - (int)(_image.Width * _zoom)) / 2;
        int y = (this.Height - (int)(_image.Height * _zoom)) / 2;
        if (this.HScroll)
            x = 0;
        if (this.VScroll)
            y = 0;
        // Calculate the panning amount to keep the image centered
        Matrix mx = new Matrix(_zoom, 0, 0, _zoom, x, y);
        mx.Translate(this.AutoScrollPosition.X / _zoom - x, this.AutoScrollPosition.Y / _zoom- y);
        e.Graphics.Transform = mx;
        e.Graphics.InterpolationMode = _interpolationMode;
        // Calculate the top-left corner of the image
        
        e.Graphics.DrawImage(_image, new Rectangle(x, y, this._image.Width, this._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);`
© www.soinside.com 2019 - 2024. All rights reserved.