SetPixel работает слишком медленно. Есть ли более быстрый способ рисования в растровое изображение?

У меня есть небольшая программа рисования, над которой я работаю. Я использую 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();
    }

Реализация MouseMove:

    public override void MouseMove(Point lastPos, Point currentPos, Layer currentLayer)
    {
        DrawLine(lastPos, currentPos, currentLayer);
    }

Реализация 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;
        }
    }

Реализация 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);
                }
            }
        }
    }

Большое спасибо за любую помощь, которую вы можете предоставить.

12
задан Pyro 14 October 2011 в 13:57
поделиться