如何实现图像的流畅缩放和平移?

3
我正在编写一个程序,用于显示地图,叠加在地图上的是摄像机位置及其视角的另一层。地图本身可以缩放和平移。问题在于地图文件大小相当大,缩放不流畅。
我创建了一个名为ZoomablePictureBox : PictureBox的类,以添加缩放和平移功能。我尝试了来自此处和其他论坛的不同方法进行缩放和平移,并最终得出以下结论,触发ZoomablePictureBoxOnPaint事件:
  private void DrawImgZoomed(PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            if (imgZoomed != null)
                e.Graphics.DrawImage(imgZoomed, new Rectangle(-ShiftX, -ShiftY, imgZoomed.Width, imgZoomed.Height), 0, 0, imgZoomed.Width, imgZoomed.Height, GraphicsUnit.Pixel);

    }

ShiftX和ShiftY提供适当的地图平移(对于此问题无关)。

imgZoomed是原始地图的缩放版本,每次缩放更改时在BackgroundWorker中计算:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
    {

        Bitmap workerImage = e.Argument as Bitmap;
        Bitmap result;

        result = new Bitmap(workerImage, new Size((int)(workerImage.Width * Zoom), (int)(workerImage.Height * Zoom)));

        e.Result = result;
    }

目前的做法是,每次用户滚动鼠标,都会根据当前缩放计算新的imgZoomed。由于地图大小约为30 MB,这可能需要长达0.5秒的时间,这很烦人,但平移运行平稳。

我意识到这可能不是最好的想法。在以前的方法中,我没有在每次滚动鼠标时创建缩放后的图像副本,而是这样做:

e.Graphics.DrawImage(Image, new Rectangle(-ShiftX, -ShiftY, (int)(this._image.Width * Zoom), (int)(this._image.Height * Zoom)), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel);

Zoom是更加流畅的,因为据我所知,它只是拉伸原始图像。另一方面,平移会跳跃严重。
我想过:
创建每个缩放级别的原始地图副本存储在内存/硬盘中 - 这将占用太多的内存/硬盘空间。
为下一个/当前/前一个缩放级别创建原始地图的副本,以便我有更多时间计算下一步 - 如果用户一次滚动多个步骤,则这种方法将无济于事。
我也尝试了矩阵变换 - 没有真正的性能提升,并且计算平移真的很麻烦。
我陷入了困境,不知道该怎么做。如果我在默认的Windows图片查看器中打开地图,缩放和平移就会很平滑。他们是如何做到这一点的呢?
如何同时实现平稳的缩放和平移?

我最近实现了类似的功能,在进行了多次测试后,我发现使用缓存的GraphicsHBitmap对象以及StretchBlt可以获得迄今为止最流畅的性能。这帮助我在7000X7000像素的图像上实现了超级流畅的显示。 - Rotem
1个回答

2
抱歉这篇文章有点长,我只保留了必要的部分。
大部分的p/invoke内容来自pinvoke.net
我的解决方案是使用StretchBlt gdi方法来实现对大图像的平滑移动,而不是使用Graphics.DrawImage。我创建了一个静态类来组合所有本地blitting操作。
另一件非常有帮助的事情是缓存BitmapHBitmapGraphics对象。
public class ZoomPanWindow
{
    private Bitmap map;
    private Graphics bmpGfx;
    private IntPtr hBitmap;

    public Bitmap Map
    {
        get { return map; }
        set 
        {
            if (map != value)
            {
                map = value;
                //dispose/delete any previous caches
                if (bmpGfx != null) bmpGfx.Dispose();
                if (hBitmap != null) StretchBltHelper.DeleteObject(hBitmap);
                if (value == null) return;
                //cache the new HBitmap and Graphics.
                bmpGfx = Graphics.FromImage(map);
                hBitmap = map.GetHbitmap();
             }
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (map == null) return;
        //finally, the actual painting!
        Rectangle mapRect = //whatever zoom/pan logic you implemented.
        Rectangle thisRect = new Rectangle(0, 0, this.Width, this.Height);
        StretchBltHelper.DrawStretch(
            hBitmap,
            bmpGfx,
            e.Graphics,
            mapRect,
            thisRect);
    }
}

public static class StretchBltHelper
{
    public static void DrawStretch(IntPtr hBitmap, Graphics srcGfx, Graphics destGfx,
        Rectangle srcRect, Rectangle destRect)
    {
        IntPtr pTarget = destGfx.GetHdc();
        IntPtr pSource = CreateCompatibleDC(pTarget);
        IntPtr pOrig = SelectObject(pSource, hBitmap);
        if (!StretchBlt(pTarget, destRect.X, destRect.Y, destRect.Width, destRect.Height,
            pSource, srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height,
            TernaryRasterOperations.SRCCOPY))
        throw new Win32Exception(Marshal.GetLastWin32Error());

        IntPtr pNew = SelectObject(pSource, pOrig);
        DeleteDC(pSource);
        destGfx.ReleaseHdc(pTarget);
    }

    [DllImport("gdi32.dll", EntryPoint = "SelectObject")]
    public static extern System.IntPtr SelectObject(
        [In()] System.IntPtr hdc,
        [In()] System.IntPtr h);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern bool DeleteDC(IntPtr hdc);

    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteObject(
        [In()] System.IntPtr ho);

    [DllImport("gdi32.dll", ExactSpelling = true, SetLastError = true)]
    static extern IntPtr CreateCompatibleDC(IntPtr hdc);

    [DllImport("gdi32.dll")]
    static extern bool StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest,
        int nWidthDest, int nHeightDest,
        IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc,
        TernaryRasterOperations dwRop);

    public enum TernaryRasterOperations : uint
    {
        SRCCOPY = 0x00CC0020
        //there are many others but we don't need them for this purpose, omitted for brevity
    }
}

您好。请问您能演示如何在Visual Studio 2019中使用这个吗?非常感谢。 - suchislife

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接