图形.DrawImage速度

本文关键字:速度 DrawImage 图形 | 更新日期: 2023-09-27 17:51:19

在我的程序中,我正在编写一个基本的图像编辑器。这部分允许用户绘制一个矩形区域,然后我弹出一个显示该区域缩放3倍左右的显示器(他们可以使用鼠标滚轮进一步调整)。如果他们右击并拖动该图像,它将移动原始图像周围的缩放区域,基本上就像放大镜一样。

问题是,我看到一些严重的性能问题,即使在相对较小的位图。如果显示缩放区域的位图大约是400x400,它仍然会以鼠标移动的速度更新,并且非常平滑,但如果我将鼠标滚轮放大到450x450左右,它会立即开始分块,如果是这样的话,它只会下降到每秒2次更新。我不明白为什么这么小的增加会导致如此巨大的性能问题……好像我的内存受到了限制之类的。放大的源位图的大小似乎并不重要,重要的是放大的位图的大小。

问题是我正在使用图形。DrawImage和一个PictureBox。通过阅读这个站点,我发现这两种方法的性能通常都不是很好,但是我对GDI的内部工作原理了解不够,无法提高我的速度。我希望你们中的一些人可能知道我的瓶颈在哪里,因为我可能只是以糟糕的方式使用这些工具,或者不知道更好的工具来代替它。

下面是我的鼠标事件和相关函数的一些片段。

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        else if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            // slide the zoomed part to look at a different area of the original image
            if (zoomFactor > 1)
            {
                isMovingZoom = true;
                // try saving the graphics object?? are these settings helping at all??
                zoomingGraphics = Graphics.FromImage(displayImage);
                zoomingGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
                zoomingGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                zoomingGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
                zoomingGraphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
            }
        }
    }

private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMovingZoom)
        {
            // some computation on where they moved mouse ommitted here
            zoomRegion.X = originalZoomRegion.X + delta.X;
            zoomRegion.Y = originalZoomRegion.Y + delta.Y;
            zoomRegionEnlarged = scaleToOriginal(zoomRegion);
            // overwrite the existing displayImage to prevent more Bitmaps being allocated
            createZoomedImage(image.Bitmap, zoomRegionEnlarged, zoomFactor, displayImage, zoomingGraphics);
        }
    }
private void createZoomedImage(Bitmap source, Rectangle srcRegion, float zoom, Bitmap output, Graphics outputGraphics)
    {
        Rectangle destRect = new Rectangle(0, 0, (int)(srcRegion.Width * zoom), (int)(srcRegion.Height * zoom));
            outputGraphics.DrawImage(source, destRect, srcRegion, GraphicsUnit.Pixel);
        if (displayImage != originalDisplayImage && displayImage != output)
            displayImage.Dispose();
        setImageInBox(output);
    }
// sets the picture box image, as well as resizes the window to fit
    void setImageInBox(Bitmap bmp)
    {
        pictureBox.Image = bmp;
        displayImage = bmp;
        this.Width = pictureBox.Width + okButton.Width + SystemInformation.FrameBorderSize.Width * 2 + 25;
        this.Height = Math.Max(450, pictureBox.Height) + SystemInformation.CaptionHeight + SystemInformation.FrameBorderSize.Height * 2 + 20;
    }
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        else if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            if (isMovingZoom)
            {
                isMovingZoom = false;
                zoomingGraphics.Dispose();
            }
        }
    }

正如你所看到的,我并没有在每次我想画东西的时候声明一个新的Bitmap,我重用了一个旧的Bitmap(和Bitmap的图形对象),尽管我不知道调用graphics是否有很多成本。FromImage反复)。我试着添加秒表来测试我的代码,但我认为DrawImage将功能传递给另一个线程,所以该函数声称相对较快。当我不使用它们时,我试图Dispose所有的位图和图形对象,并避免在MouseMove事件期间重复调用分配/释放资源。我使用的是PictureBox,但我不认为这是这里的问题。

任何帮助加快这段代码或教我什么是发生在DrawImage感激!我已经修剪了一些多余的代码,使其更美观,但如果我不小心修剪了一些重要的东西,或者没有显示我是如何使用可能导致问题的东西,请让我知道,我会修改这篇文章。

图形.DrawImage速度

我处理这样的问题的方式是当接收到Paint事件时,我将整个图像绘制到内存位图,然后将其BLT到窗口。这样,所有的视觉闪光都被消除了,它看起来快,即使它实际上不是。

为了更清楚,我没有从鼠标事件处理程序中进行任何绘画。我只是设置了主Paint处理程序所需的内容,然后执行Invalidate。因此,绘制发生在鼠标事件完成之后。


补充:在评论中回答Tom的问题,我是这样做的。记住,我并不是说它快,只是说它看起来快,因为_e.Graphics.DrawImage(bmToDrawOn, new Point(0,0));是瞬间出现的。它只是从一个图像到下一个图像。用户不会看到窗口被清除,然后逐件重新绘制。它的效果与双缓冲相同。

    Graphics grToDrawOn = null;
    Bitmap bmToDrawOn = null;
    private void DgmWin_Paint(object sender, PaintEventArgs _e){
        int w = ClientRectangle.Width;
        int h = ClientRectangle.Height;
        Graphics gr = _e.Graphics;
        // if the bitmap needs to be made, do so
        if (bmToDrawOn == null) bmToDrawOn = new Bitmap(w, h, gr);
        // if the bitmap needs to be changed in size, do so
        if (bmToDrawOn.Width != w || bmToDrawOn.Height != h){
            bmToDrawOn = new Bitmap(w, h, gr);
        }
        // hook the bitmap into the graphics object
        grToDrawOn = Graphics.FromImage(bmToDrawOn);
        // clear the graphics object before drawing
        grToDrawOn.Clear(Color.White);
        // paint everything
        DoPainting();
        // copy the bitmap onto the real screen
        _e.Graphics.DrawImage(bmToDrawOn, new Point(0,0));
    }
    private void DoPainting(){
        grToDrawOn.blahblah....
    }