mirror of
https://github.com/DarthAffe/HPPH.git
synced 2026-11-09 18:36:34 +00:00
Added extensions for SkiaSharp
This commit is contained in:
1 parent
2c401f0eb1
commit
5126980e97
5 files changed
+76
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="SkiaSharp" Version="2.88.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HPPH\HPPH.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,32 @@
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace HPPH.SkiaSharp;
|
||||
|
||||
public static class ImageExtension
|
||||
{
|
||||
public static unsafe SKImage ToSKImage(this IImage image) => SKImage.FromBitmap(image.ToSKBitmap());
|
||||
|
||||
public static unsafe SKBitmap ToSKBitmap(this IImage image)
|
||||
{
|
||||
SKBitmap bitmap = new(image.Width, image.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul);
|
||||
nint pixelPtr = bitmap.GetPixels(out nint length);
|
||||
image.ConvertTo<ColorBGRA>().CopyTo(new Span<byte>((void*)pixelPtr, (int)length));
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static byte[] ToPng(this IImage image)
|
||||
{
|
||||
using SKImage skImage = image.ToSKImage();
|
||||
return skImage.Encode(SKEncodedImageFormat.Png, 100).ToArray();
|
||||
}
|
||||
|
||||
public static IImage ToImage(this SKImage skImage) => SKBitmap.FromImage(skImage).ToImage();
|
||||
|
||||
public static IImage ToImage(this SKBitmap bitmap)
|
||||
=> Image<ColorBGRA>.Create(MemoryMarshal.Cast<SKColor, ColorBGRA>(bitmap.Pixels), bitmap.Width, bitmap.Height);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using SkiaSharp;
|
||||
|
||||
namespace HPPH.SkiaSharp;
|
||||
|
||||
public static class ImageHelper
|
||||
{
|
||||
public static IImage LoadImage(string path)
|
||||
{
|
||||
using SKImage image = SKImage.FromEncodedData(path);
|
||||
return image.ToImage();
|
||||
}
|
||||
|
||||
public static IImage LoadImage(Stream stream)
|
||||
{
|
||||
using SKImage image = SKImage.FromEncodedData(stream);
|
||||
return image.ToImage();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user