1

I have a big problems with performance, when it comes to draw a lot of figures in Metro app. I've tried:

  • adding Rectangles to Grid (terrible idea, don't do that),
  • using this library: http://writeablebitmapex.codeplex.com/ (original WriteableBitmap for Win8 doesn't provide drawing functionality).

I'm working on the "Game of life" app, which basically needs to draw at least 32x32 rectangles in very small time. I think C# + XAML isn't suitable for such applications, but I don't have time to learn DirectX by now, which would be probably the best choice.

The drawing itself is pretty straightforward, here's the code:

public void DrawFieldArray()
{
    Bitmap.Clear(Color.FromArgb(100, 100, 100, 100));
    Int32 ArraySize = FieldArray.GetLength(0);

    for (int i = 0; i < ArraySize; ++i)
    {
        for (int j = 0; j < ArraySize; ++j)
        {
            Field CurrentField = FieldArray[i, j];
            Bitmap.FillRectangle(CurrentField.Pos.Column + 1,
                                 CurrentField.Pos.Row + 1,
                                 CurrentField.Pos.Column + Field.CellSize - 1,
                                 CurrentField.Pos.Row + Field.CellSize - 1,
                                 (CurrentField.IsAlive ? Field.CellAlive : Field.CellDead));
        }
    }
}

Bitmap is an instance of WriteableBitmap class and it's shown on Image control. Field.CellAlive and Field.CellDead are predefined colors. This code works very slow and I have no idea, how to improve the performance. I've tried to draw this in background and then show it, but it's the same in case of effectivity. If you know any good drawing libraries or tricks to make it faster, I would be very grateful. It's hard to find something in Google about this subject.

--edit--

Got it, finally. All I had to do was to use GetBitmapContext() method:

using (Bitmap.GetBitmapContext())
{
    for (int i = 0; i < ArraySize; ++i)
    {
        for (int j = 0; j < ArraySize; ++j)
        {
            Field CurrentField = FieldArray[i, j];
            Bitmap.FillRectangle(CurrentField.Pos.Column + 1,
                                 CurrentField.Pos.Row + 1,
                                 CurrentField.Pos.Column + Field.CellSize - 1,
                                 CurrentField.Pos.Row + Field.CellSize - 1,
                                 (CurrentField.IsAlive ? Field.CellAlive : Field.CellDead));
        }
    }
}

Now it works perfectly :) Here's the link to the solution: http://writeablebitmapex.codeplex.com/discussions/432111.

  • Nested for loop...It's going to be slower xD – Adam Aug 13 '14 at 13:02
  • Yeah, but not THAT slow! It takes about a second to draw 8x8 rectangles, I really don't think it's because of for loop. – Bartłomiej Zieliński Aug 13 '14 at 13:18
  • Well, by slow, how slow is slow? – Adam Aug 13 '14 at 13:22
  • If you have to wait one second for 64 rectangles to be drawn, that's my definition of too slow. It should be like 50-100 times faster to say it's ok. Every step of this application is basically drawing the whole board again and again (with some changes in colors), so it should be updated more than once in a second. I've written it in C++ Builder, and it worked flawlessly. Now, when I'm trying to do pretty much the same in C# with XAML for Windows 8, it's horrible. – Bartłomiej Zieliński Aug 13 '14 at 15:14

0 Answers0