0

Is there a way to convert a Windows.UI.Xaml.Media.Imaging.BitmapSource object into a member of a class that implements Microsoft.Graphics.Canvas.ICanvasImage? The interface itself consists of just two rectangle-getting methods. However, I assume that's not enough, as my ultimate goal is to call

CanvasDrawingSession.DrawImage()

which I have to imagine requires a way to get at the bitmap pixels.

William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • Where does your *BitmapSource* come from? – Romasz Dec 09 '16 at 15:54
  • Splat library. https://github.com/paulcbetts/splat -- the fundamental issue that makes things hard is that I am developing for multiple platforms. But I ended up deciding that this was messy enough that it was better to bypass the BitmapSource altogether in the case of Windows. – William Jockusch Dec 09 '16 at 18:56
  • I ask, hence maybe it will be easier to use *SoftwareBitmap* instead of *BitmapSource*. Therefore it's straight away to create *CanvasImage* from it. – Romasz Dec 09 '16 at 19:01
  • Well, the idea was that Splat loading hides the platform dependencies. But in this case, that's not going to fly, so I'm just getting it from the stream. – William Jockusch Dec 09 '16 at 19:03
  • 2
    If you have a stream then you can probably use *SoftwareBitmap*: `BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); return await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Rgba16, BitmapAlphaMode.Premultiplied);`. Then you can use *CanvasBitmap.CreateFromSoftwareBitmap(...)`. – Romasz Dec 09 '16 at 19:29

2 Answers2

1

You cannot get pixel from Windows.UI.Xaml.Media.Imaging.BitmapImage directly. But you can use a WriteableBitmap instead of a BitmapImage. Then you can get pixel data via its PixelBuffer property.

After that, you could use CanvasBitmap.CreateFromBytes() method to create CanvasBitmap. Then using this CanvasBitmap object as parameter to call CanvasDrawingSession.DrawImage() method.

Xie Steven
  • 8,544
  • 1
  • 9
  • 23
1

Apart from using PixelBuffer, as described by Xavier Xie above, you may use SoftwareBitmap to create your ICanvasImage. If you have a stream then you can do this like this:

BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
CanvasDrawingSession.DrawImage(await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Rgba16, BitmapAlphaMode.Premultiplied));
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154