Browse Source

Fixed bitmap loading tests

Wiesław Šoltés 9 years ago
parent
commit
e5c8b7c46d
1 changed files with 21 additions and 14 deletions
  1. 21 14
      src/Skia/Avalonia.Skia/PlatformRenderInterface.cs

+ 21 - 14
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)