From e5c8b7c46d5f45627b1b765878dda79904a7ee80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Wed, 3 Aug 2016 20:58:57 +0200 Subject: [PATCH] Fixed bitmap loading tests --- .../Avalonia.Skia/PlatformRenderInterface.cs | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index 1c1ebf4a08..cb6d0547e9 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -28,28 +28,35 @@ namespace Avalonia.Skia return new StreamGeometryImpl(); } - IBitmapImpl LoadBitmap(byte[] data) - { - var bitmap = SKBitmap.Decode(data); - if (bitmap == null) - { - throw new ArgumentException("Unable to load bitmap from provided data"); - } - - return new BitmapImpl(bitmap); - } - public IBitmapImpl LoadBitmap(System.IO.Stream stream) { - using (var sr = new BinaryReader(stream)) + using (var s = new SKManagedStream(stream)) { - return LoadBitmap(sr.ReadBytes((int)stream.Length)); + using (var codec = SKCodec.Create(s)) + { + var info = codec.Info; + var bitmap = new SKBitmap(info.Width, info.Height, SKImageInfo.PlatformColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul); + + IntPtr length; + var result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out length)); + if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput) + { + return new BitmapImpl(bitmap); + } + else + { + throw new ArgumentException("Unable to load bitmap from provided data"); + } + } } } public IBitmapImpl LoadBitmap(string fileName) { - return LoadBitmap(File.ReadAllBytes(fileName)); + using (var stream = File.OpenRead(fileName)) + { + return LoadBitmap(stream); + } } public IRenderTargetBitmapImpl CreateRenderTargetBitmap(int width, int height)