如何防止在滚动一个大的缩小图片时出现图形口吃

本文关键字:缩小 图形 滚动 何防止 一个 | 更新日期: 2023-09-27 18:04:12

我有一张大的TIFF图片(5.9 MB, 13k X 16k分辨率),用户可以将其加载到一个可滚动的面板中,然后他可以放大/缩小,滚动和标记点,区域等。

对于可滚动的双缓冲面板,我使用了Bob Powell的ZoomPicBox的修改面板只显示当前视图中图片的一部分。

缩放时滚动图像时出现口吃(即使interpolationMode设置为low)

有什么可以做的(最好没有硬件加速)?

面板的油漆事件:

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        if (_image == null)
        {
            base.OnPaintBackground(e);
            return;
        }
        //scale
        System.Drawing.Drawing2D.Matrix ScaleMat = new System.Drawing.Drawing2D.Matrix(_zoom, 0, 0, _zoom, 0, 0);
        //move to position of scrollbas
        ScaleMat.Translate(this.AutoScrollPosition.X / (_zoom), this.AutoScrollPosition.Y / (_zoom));

        e.Graphics.Transform = ScaleMat;
        e.Graphics.InterpolationMode = _interpolationMode;
        e.Graphics.DrawImage(_image, new Rectangle(0, 0, _image.Width, _image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);
        
        base.OnPaint(e);
    }

* _zoom、_image和_interpolationMode是控件的私有字段。

构造函数:

 public PicBoxPlus()
        {
            MouseMove += PicBoxPlus_MouseMove;
            KeyDown += PicBoxPlus_KeyDown;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            this.AutoScroll = true;
        }

然后我尝试实现Sinatr的代码,但有些东西是错误的,因为我得到的是一个黑色的图像(大小合适)。有人知道是哪里出了问题吗?

新的paint事件:

 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (mCachedImage == null)
            {
                base.OnPaintBackground(e);
                return;
            }
            //scale
            System.Drawing.Drawing2D.Matrix ScaleMat = new System.Drawing.Drawing2D.Matrix(mZoom, 0, 0, mZoom, 0, 0);
            //move to position of scrollbas
            ScaleMat.Translate(this.AutoScrollPosition.X / (mZoom), this.AutoScrollPosition.Y / (mZoom));
            try
            {
                if (mCachedImage == null)
                {
                    mCachedImage = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
                    using (var cacheGraphics = Graphics.FromImage(mCachedImage))
                    {
                        cacheGraphics.Transform = ScaleMat;
                        cacheGraphics.InterpolationMode = _interpolationMode;
                        cacheGraphics.DrawImage(mCachedImage, new Rectangle(0, 0, mCachedImage.Width, mCachedImage.Height), 0, 0, mCachedImage.Width, mCachedImage.Height, GraphicsUnit.Pixel);
                    }
                    e.Graphics.DrawImage(mCachedImage, Point.Empty);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
           
            base.OnPaint(e);
        }

图像和缩放属性:

public Bitmap Image
    {
        get { return mCachedImage; }
        set
        {
            mCachedImage = value;
            UpdateScaleFactor();
            this.Invalidate();
        }
    }
public Single Zoom
{
    get { return mZoom; }
    set
    {
        if (value <= 0||value < 0.001)
        {
            value = 0.001f;
        }
        mZoom = value;
        UpdateScaleFactor();
        ResetCache(); // Sinatr's function
        this.Invalidate();
    }
}

从主表单加载图像:

panelMap.Image = (Bitmap)Image.FromFile("pic.tiff");

如何防止在滚动一个大的缩小图片时出现图形口吃

没有经过测试,但应该给出一个想法。

Bitmap _cached = null;
override void OnPaint(PaintEventArgs e)
{
    if(_cached == null)
    {
        _cached = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
        using(var graphics = Graphics.FromImage(_cached)
        {
             // draw into this graphics once -> it will be cached in _cached bitmap
        }
    }
    e.Graphics.DrawImage(_cached, Point.Empty);
}
// call this if _zoom or ClientSize is changed
void ResetCache()
{
    _cache = null;
    this.Invalidate(); // mandatory for _zoom change
}

另外,我不知道如何展示你的放大图片,但通常有一个偏移,这样你就可以移动(平移)图像。