How to access pixels behind IImageProvider (Nokia Imaging SDK on WP8)

723 views Asked by At

I have an image source behind IImageProvider interface, and I'm trying to access its pixels.

There is a method inside IImageProvider: imageProvider.GetBitmapAsync(bitmapToFill)

  • I can't get WriteableBitmap because I'm running on a non UI thread. I can't instantiate one empty WriteableBitmap to write to which is unfortunate because I can access pixels from it..
  • I can fill a Bitmap object with the data, but there is no way to access its pixels on Windows Phone (it is missing system.drawing...)

How can I access individual pixels of the source behind IImageProvider?

2

There are 2 answers

2
Igor Ralic On BEST ANSWER

Have you tried this?

var bitmap = await imageSource.GetBitmapAsync(null, OutputOption.PreserveAspectRatio);
var pixels = bitmap.Buffers[0];
for (uint i = 0; i < pixels.Buffer.Length; i++)
    {
        var val = pixels.Buffer.GetByte(i);
    }
  • i = R ... [0]
  • i+1 = G ... [1]
  • i+2 = B ... [2]
  • i+3 = A ... [3]

and so on

imageSource is your IImageProvider, I tested it with BufferImageSource.

1
CÅdahl On

A slightly more efficient option is this.

Create a Bitmap over your own .NET array of (empty) pixels, then using GetBitmapAsync to fill that. Once rendering is done, you can find the result in the original array you passed.

byte[] myPixels = new byte[correctSize]; // You can use a ColorModeDescriptor to calculate the size in bytes here.

using(var wrapperBitmap = new Bitmap(widthAndHeight, colorMode, pitch, myPixels.AsBuffer()))
{
    await interestingImage.GetBitmapAsync(wrapperBitmap, OutputOption.PreserveAspectRatio);
}

// and use myPixels here.