windows-runtimewriteablebitmapexwin-universal-app

How to get pixel color from a Point in image with WriteableBitmapEx in WinRT?


I'm porting my current (Silverlight) WP8.0 app to a Universal Windows application. In this application I have a control that allows the user to use a custom dial control. When the dial stops, the app should get the pixel color from that specific point (and underlaying image).

In WP8.0, I used to do the following:

WriteableBitmap wb = new WriteableBitmap(ColorWheelImage, null);
Color c = wb.GetPixel(pointX, pointY);

ColorWheelImage is in the XAML.

In WinRT the WriteableBitmap only support new WriteableBitmap(int pixelWidth, int pixelWidth) and not setting an image like in Silverlight.

How can I fix this.. I can't seem to figure it out :(!

Thanks, Niels


Solution

  • In WinRT, WriteableBitmap is unable to draw visuals. Instead, MS added new control RenderTargetBitmap.

    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); 
    await renderTargetBitmap.RenderAsync(ColorWheelImage, width, height);
    IBuffer pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
    

    But GetPixelsAsync() gives you IBuffer, instead of int array. To get an ordinal WriteableBitmap, you can use WriteableBitmapEx.

    var width = renderTargetBitmap.PixelWidth;
    var height = renderTargetBitmap.PixelHeight;
    var writeableBitmap = await new WriteableBitmap(1, 1).FromPixelBuffer(pixelBuffer, width, height);