SetPixel太慢了.是否有更快的方法来绘制位图?

Pyr*_*yro 12 c# graphics gdi bitmap layer

我正在制作一个小型油漆程序.我在位图上使用SetPixel来绘制线条.当画笔大小变大时,如同25个像素一样,会有明显的性能下降.我想知道是否有更快的方法来绘制位图.以下是该项目的背景:

  • 我正在使用位图,以便我可以利用图层,如Photoshop或GIMP.
  • 正在手动绘制线条,因为这最终将使用图形输入板压力来改变线条长度的大小.
  • 这些线最终应沿着边缘进行抗变形/平滑处理.

我将包括我的绘图代码,以防万一这是缓慢而不是Set-Pixel位.

这是在绘画发生的窗口:

    private void canvas_MouseMove(object sender, MouseEventArgs e)
    {
        m_lastPosition = m_currentPosition;
        m_currentPosition = e.Location;

        if(m_penDown && m_pointInWindow)
            m_currentTool.MouseMove(m_lastPosition, m_currentPosition, m_layer);
        canvas.Invalidate();
    }
Run Code Online (Sandbox Code Playgroud)

MouseMove的实现:

    public override void MouseMove(Point lastPos, Point currentPos, Layer currentLayer)
    {
        DrawLine(lastPos, currentPos, currentLayer);
    }
Run Code Online (Sandbox Code Playgroud)

DrawLine的实现:

    // The primary drawing code for most tools. A line is drawn from the last position to the current position
    public override void DrawLine(Point lastPos, Point currentPos, Layer currentLayer)
    {
        // Creat a line vector
        Vector2D vector = new Vector2D(currentPos.X - lastPos.X, currentPos.Y - lastPos.Y);

        // Create the point to draw at
        PointF drawPoint = new Point(lastPos.X, lastPos.Y);

        // Get the amount to step each time
        PointF step = vector.GetNormalisedVector();

        // Find the length of the line
        double length = vector.GetMagnitude();

        // For each step along the line...
        for (int i = 0; i < length; i++)
        {
            // Draw a pixel
            PaintPoint(currentLayer, new Point((int)drawPoint.X, (int)drawPoint.Y));
            drawPoint.X += step.X;
            drawPoint.Y += step.Y;
        }
    }
Run Code Online (Sandbox Code Playgroud)

PaintPoint的实现:

    public override void PaintPoint(Layer layer, Point position)
    {
        // Rasterise the pencil tool

        // Assume it is square

        // Check the pixel to be set is witin the bounds of the layer

            // Set the tool size rect to the locate on of the point to be painted
        m_toolArea.Location = position;

            // Get the area to be painted
        Rectangle areaToPaint = new Rectangle();
        areaToPaint = Rectangle.Intersect(layer.GetRectangle(), m_toolArea);

            // Check this is not a null area
        if (!areaToPaint.IsEmpty)
        {
            // Go through the draw area and set the pixels as they should be
            for (int y = areaToPaint.Top; y < areaToPaint.Bottom; y++)
            {
                for (int x = areaToPaint.Left; x < areaToPaint.Right; x++)
                {
                    layer.GetBitmap().SetPixel(x, y, m_colour);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

非常感谢您提供的任何帮助.

Jac*_*ack 14

您可以锁定位图数据并使用指针手动设置值.它快得多.虽然你将不得不使用不安全的代码.

public override void PaintPoint(Layer layer, Point position)
    {
        // Rasterise the pencil tool

        // Assume it is square

        // Check the pixel to be set is witin the bounds of the layer

        // Set the tool size rect to the locate on of the point to be painted
        m_toolArea.Location = position;

        // Get the area to be painted
        Rectangle areaToPaint = new Rectangle();
        areaToPaint = Rectangle.Intersect(layer.GetRectangle(), m_toolArea);

        Bitmap bmp;
        BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        int stride = data.Stride;
        unsafe
        {
            byte* ptr = (byte*)data.Scan0;
            // Check this is not a null area
            if (!areaToPaint.IsEmpty)
            {
                // Go through the draw area and set the pixels as they should be
                for (int y = areaToPaint.Top; y < areaToPaint.Bottom; y++)
                {
                    for (int x = areaToPaint.Left; x < areaToPaint.Right; x++)
                    {
                        // layer.GetBitmap().SetPixel(x, y, m_colour);
                        ptr[(x * 3) + y * stride] = m_colour.B;
                        ptr[(x * 3) + y * stride + 1] = m_colour.G;
                        ptr[(x * 3) + y * stride + 2] = m_colour.R;
                    }
                }
            }
        }
        bmp.UnlockBits(data);
    }
Run Code Online (Sandbox Code Playgroud)

  • 您需要指定http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx以包含alpha组件.只需将`PixelFormat.Format24bppRgb`更改为'PixelFormat.Format32bppArgb`即可.我相信它应该在R之后,所以只是将它偏移+3.如果每个像素使用32位,则需要将"x*3"更改为"x*4",因为现在每个像素将为4个字节. (2认同)

fix*_*gon 5

SetPixel执行此操作:锁定整个图像,设置像素并解锁它

尝试这样做:使用lockbits获取整个内存映像的锁定,处理更新并释放锁定.

lockbits

  • 3年后,仍然没有例子 (21认同)
  • @rr-它只是一个很长的时间 (3认同)