Skip to content

Commit

Permalink
Merge pull request #16 from rzhw/patch-2
Browse files Browse the repository at this point in the history
Load to WriteableBitmap instead of BitmapImage on WinRT
  • Loading branch information
Paul Betts committed Jul 28, 2013
2 parents d1d9814 + d43dafe commit 89ef574
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions Splat/WinRT/Bitmaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@ class PlatformBitmapLoader : IBitmapLoader
{
public async Task<IBitmap> Load(Stream sourceStream, float? desiredWidth, float? desiredHeight)
{
var source = new BitmapImage();
using (var rwStream = new InMemoryRandomAccessStream()) {
await sourceStream.CopyToAsync(rwStream.AsStreamForWrite());

if (desiredWidth != null) {
source.DecodePixelWidth = (int)desiredWidth;
source.DecodePixelHeight = (int)desiredHeight;
}
var decoder = await BitmapDecoder.CreateAsync(rwStream);

var transform = new BitmapTransform();
if (desiredWidth != null) {
transform.ScaledWidth = (uint)desiredWidth;
transform.ScaledHeight = (uint)desiredHeight;
}

// NB: WinRT is dumb.
var rwStream = new InMemoryRandomAccessStream();
await rwStream.AsStreamForRead().CopyToAsync(rwStream.AsStreamForWrite());
var pixelData = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb);
var pixels = pixelData.DetachPixelData();

await source.SetSourceAsync(rwStream);
return (IBitmap) new BitmapImageBitmap(source);
WriteableBitmap bmp = new WriteableBitmap((int)decoder.OrientedPixelWidth, (int)decoder.OrientedPixelHeight);
using (var bmpStream = bmp.PixelBuffer.AsStream()) {
bmpStream.Seek(0, SeekOrigin.Begin);
bmpStream.Write(pixels, 0, (int)bmpStream.Length);
return (IBitmap) new WriteableBitmapImageBitmap(bmp);
}
}
}

public Task<IBitmap> LoadFromResource(string resource, float? desiredWidth, float? desiredHeight)
Expand Down Expand Up @@ -144,4 +152,4 @@ public static BitmapSource ToNative(this IBitmap This)
}
}

}
}

0 comments on commit 89ef574

Please sign in to comment.