0

I was just wondering if anyone knew (performance wise on wp7) the best way to update a WriteableBitmap pixel by pixel... I can see the Pixels.SetValue method which can take in a colour and a single location. But surely a faster option is to set an array first, then SetValue(theArray) all at once... something like this...

Int32[] pixels = bmp.Pixels;
for(Int32 y = 479; y > 0; y--)
    for (Int32 x = 0; x < 480; x++)
    {
        pixels[x + (y * 480)] = 0;
    }
bmp.Pixels.SetValue(pixels);

but there doesnt appear to be that option???

objviously here I am just setting everything to black... but if I want to do more...

So basically, Im just asking what is the best way to manipulate/create a bitmap pixel by pixel and get a best performance? (think updateing the bitmap on every tick)

edit: I just noticed that Texture2D.SetData<Int32>(theArray) has this functionality, can anyone comment on the performance of this? or perhaps recommend a better way of doing this?

Wacka
  • 87
  • 1
  • 9
  • I don't think a bitmap and great performance go along. Do you want to refresh the entire screen at 60fps? – Dykam Feb 17 '12 at 11:56
  • A method doesn't become magically faster just because you pass an array. WriteableBitmap.Pixels is just an array, the processing is done when you try to generate the actual image from the WriteableBitmap. So writing yourself all the values in the Pixels array, or calling a method which will copy the contents of an array in the Pixels array is about the same performance-wise. – Kevin Gosse Feb 17 '12 at 12:31
  • Ive changed it slightly.. the UpdateInterval is set to TimeSpan(60000) and I just manipulate a Int32[] _pixels = new Int32[230400] and in the draw event I just call myTexture2D.SetData(_pixels) it seems quite smooth... I havent tested it on a device yet though... – Wacka Feb 17 '12 at 12:31
  • Ok, I made the gameTimer.UpdateInterval = TimeSpan.Zero ... now its like... super fast :D putting a low number in the TimeSpan seemed to cause lag... I guess when its TimeSpan.Zero it does something different... now I'll have to put in my own delays because its too fast ;) – Wacka Feb 24 '12 at 09:33

1 Answers1

1

You can create a Bitmap giving it an array of bytes. One of the constructors of Bitmap allows this. Look at this and this.

So probably the best way is to operate on an array and create a Bitmap out of it when needed. But of course to draw lines, circles etc. you would be better off using some drawing functions.

Community
  • 1
  • 1
Maciej
  • 7,871
  • 1
  • 31
  • 36
  • ah nice, it would be interesting to see if this is faster than updating a Texture2D (although I would guess that the creation of a new Bitmap Class every frame would take up a few cycles?) – Wacka Feb 17 '12 at 16:26
  • its interesting that you can use the IntPtr struct, but you cant use unsafe code, and therefore cant create pointers in the classic sense (i.e. Int32* myInt) in windows phone 7 – Wacka Feb 17 '12 at 16:30
  • Modifying Bitmap pixel by pixel feels very slow. If you want to do it faster use as simple and as raw method as possible. Best way to find out is to run some tests. – Maciej Feb 21 '12 at 19:04
  • yeah it looks like the best I can get is around a 240x300 texture... then I non-uniform scale it in the spritebatch.Draw method... and the UpdateInterval is set to 66666 – Wacka Feb 22 '12 at 15:47