1

Necessary to draw by pixels array with data (large). How can I do that?

I tried a Canvas and Rectangle on it - the computer hanged himself ... Tried the following options below (DrawingContext) - computer still hangs himself, but a little bit less.

Please recommend options with the least load on the computer. int width = 800; - the size of the array (large!!!) int size = 1; - it is desirable to be able to make the fragment is not only one but several pixels

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            Random rnd = new Random();
            int width = 800;
            int size = 1;
            CellType[,] types = new CellType[width, width];
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    int r = rnd.Next(0, 100);
                    if (r >= 70) types[j,i] = CellType.IsOccupied;
                    else types[j, i] = CellType.IsEmpty;
                }
            }


            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Brush brush = Brushes.Black;

                    switch (types[j, i])
                    {
                        case CellType.IsEmpty: brush = Brushes.Green;
                            break;
                        case CellType.IsOccupied: brush = Brushes.Black;
                            break;
                    }

                    drawingContext.DrawRectangle(brush,
                    new Pen(brush, 1),
                    new Rect(j * size, i * size, size, size));
                }
            }

            base.OnRender(drawingContext);
        }
Grenkin
  • 93
  • 1
  • 10
  • 1
    Firstly, move the pen out of the loop; make one pen per brush you could use. __Freeze each pen__. I also highly recommend you look at the `InteropBitmap` and setting the bytes manually. – Meirion Hughes Apr 09 '13 at 08:58
  • Have a look at [WriteableBitmap](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx) and use that for direct drawing. You could also use Direct2D (only vista and above) or use Direct3D. – dowhilefor Apr 09 '13 at 09:11

1 Answers1

1

For how long does it freeze? A couple of years ago I did something quite similar in Silverlight. It was a bit slow but still acceptable. Try using WriteableBitmap with SetPixel or FillRectangle methods, but please keep in mind that drawing pixel by pixel in a loop will always take some time.

Forgot to mention SetPixel and FillRectangle may need WriteableBitmapEx, available here: http://writeablebitmapex.codeplex.com/

Pawel Wrobel
  • 494
  • 3
  • 13
  • Worked perfectly. But cannot change the size of "brush". Int32Rect rect = new Int32Rect(j * size, i * size, size, size); wb.WritePixels(rect, pens[(int)types[j, i]], size*10, 0); if size is > 1 I get no image. – Grenkin Apr 09 '13 at 10:02
  • I cant help you with this. I guess this may be a problem related with the "stride" parameter. Try this [article](http://stackoverflow.com/questions/13953735/calculating-the-required-buffer-size-for-the-writeablebitmap-writepixels-method) – Pawel Wrobel Apr 09 '13 at 13:18