I have a big problems with performance, when it comes to draw a lot of figures in Metro app. I've tried:
- adding
Rectangles
toGrid
(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.