Browse Source

Merge pull request #649 from wieslawsoltes/FixSkiaLoadBitmap

Fixed bitmap loading tests
pull/653/head
Jeremy Koritzinsky 10 years ago
committed by GitHub
parent
commit
126c28862d
  1. 35
      src/Skia/Avalonia.Skia/PlatformRenderInterface.cs

35
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)

Loading…
Cancel
Save