Skip to content

Commit

Permalink
Create SkiaExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jan 4, 2020
1 parent dfd7a8d commit 1aecb22
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Svg.Skia/SkiaExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Wiesław Šoltés. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using SkiaSharp;

namespace Svg.Skia
{
public static class SkiaExtensions
{
public static SKBitmap? ToBitmap(this SKPicture skPicture, SKColor background, float scaleX, float scaleY)
{
float width = skPicture.CullRect.Width * scaleX;
float height = skPicture.CullRect.Height * scaleY;
if (width > 0 && height > 0)
{
var skImageInfo = new SKImageInfo((int)width, (int)height);
var skBitmap = new SKBitmap(skImageInfo);
using (var skCanvas = new SKCanvas(skBitmap))
{
skCanvas.Clear(SKColors.Transparent);
if (background != SKColor.Empty)
{
skCanvas.DrawColor(background);
}
skCanvas.Save();
skCanvas.Scale(scaleX, scaleY);
skCanvas.DrawPicture(skPicture);
skCanvas.Restore();
return skBitmap;
}
}
return null;
}
}
}

0 comments on commit 1aecb22

Please sign in to comment.