I downloaded WriteableBitmapEx for winrt (win 8 metro). I added a reference to the dll with the extension methods , used them in my code , every thing compiles ok.
The problem is that the extension methods have no effect. Here is part of my code:
WriteableBitmap wb = new WriteableBitmap((int)pictureSize.Width, (int)pictureSize.Height);
wb.SetSource(fileStream);
var newWB = wb.Flip(FlipMode.Vertical);
ImageControl.Source = newWB;
newWB.Invalidate();
The image does not appear on the screen. But if I try to draw something on the image , like a line, the image appears on the screen unchanged.
thank you!
I guess the image wasn't loaded fully when you use wb.Flip. IIRC wb.SetSource runs async.
There's another method available in the WriteableBitmapEx library called FromContent. With that you can load an image from the content of the app and the method handles everything for you in the background.
The below snippet just loads the logo.png, flips it and assigns the WriteableBitmap to an image control.
var wb = await BitmapFactory.New(1, 1).FromContent(new Uri(BaseUri, @"///assets/logo.png"));
wb = wb.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
Viewport.Source = wb;
If you look into the source code of the WinRT version of the WriteableBitmapEx.FromContent method you will see how a stream is transformed into the WriteableBitmap. Use the below snippet to decode any image format IRandomAccessStream into a WriteableBitmap:
// Decode image format
var decoder = await BitmapDecoder.CreateAsync(fileStream);
var transform = new BitmapTransform();
var pixelData = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
// Swap R and B channels since it's reversed
var pixels = pixelData.DetachPixelData();
for (var i = 0; i < pixels.Length; i += 4)
{
var r = pixels[i];
var b = pixels[i + 2];
pixels[i] = b;
pixels[i + 2] = r;
}
// Copy pixels to WriteableBitmap
var wb = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var bmpStream = wb.PixelBuffer.AsStream())
{
bmpStream.Seek(0, SeekOrigin.Begin);
bmpStream.Write(pixels, 0, (int)bmpStream.Length);
}
// Your original code
var newWB = wb.Flip(FlipMode.Vertical);
ImageControl.Source = newWB;
Yes, that's way more code than one would except, esp. if you did this in Silverlight or WPF before.