none
WriteableBitmap in WPF

    Question

  • I am trying to port an application from Silverlight to WPF and would like to get some help.  I am quite experienced in the former, but not so in the latter.  It turns out to be surprisingly difficult.

    I managed to get the WPF code to dual compile in Silverlight and WPF (with tons of features turned off in the WPF version, for now), but I am very much stuck with getting the equivalent of WriteableBitmap to work.

    Yes WPF supports WriteableBitmap but apparently in a very different fashion.  I did some research (including going to the WPF forum) and the answers are either old or less than satisfactory.  For example, someone wants to update some line drawings 10 times a second, and the answer was "the initial implementation of WriteableBitmap did not provide adequate performance for scenarios like yours".  Really?

    So does anyone have a solution to this problem?  Ideally it would satisfy the following:

    (1) Use WriteableBitmap or something equivalent that allows easy read/write access to the pixels, without having to copy in/out the whole thing for each access.

    (2) Allow the use of WriteableBitmap.Render() and Invalidate() or something equivalent.

    As an example, I would like to get the following code working in <1 seconds (certainly doable in Silverlight).  The code is meaningless other than to demonstrate what I would like to be able to achieve in reasonable amount of time.

    Image image = makeImage();  // some 100x100 image;
    WriteableBitmap wb = new WriteableBitmap(1024, 768);
    for (int i = 0; i < 256; i++) wb.Render(image, getTransform(i));  // draw the same image 256 times, with offsets;
    wb.Invalidate();
    int count = 0;
    for (int i = 0; i < 1024 * 768; i++)
      if (((wb.Pixels[ i ] >> 24) & 0xff) > 0) count++;



    Your help would be appreciated.  Thanks!

    Wednesday, September 29, 2010 2:48 PM

All replies