mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Added extensions for SkiaSharp
This commit is contained in:
parent
2c401f0eb1
commit
5126980e97
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\HPPH.Reference\HPPH.Reference.csproj" />
|
<ProjectReference Include="..\HPPH.Reference\HPPH.Reference.csproj" />
|
||||||
|
<ProjectReference Include="..\HPPH.SkiaSharp\HPPH.SkiaSharp.csproj" />
|
||||||
<ProjectReference Include="..\HPPH.System.Drawing\HPPH.System.Drawing.csproj" />
|
<ProjectReference Include="..\HPPH.System.Drawing\HPPH.System.Drawing.csproj" />
|
||||||
<ProjectReference Include="..\HPPH\HPPH.csproj" />
|
<ProjectReference Include="..\HPPH\HPPH.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
18
HPPH.SkiaSharp/HPPH.SkiaSharp.csproj
Normal file
18
HPPH.SkiaSharp/HPPH.SkiaSharp.csproj
Normal file
@ -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>
|
||||||
32
HPPH.SkiaSharp/ImageExtension.cs
Normal file
32
HPPH.SkiaSharp/ImageExtension.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
18
HPPH.SkiaSharp/ImageHelper.cs
Normal file
18
HPPH.SkiaSharp/ImageHelper.cs
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
HPPH.sln
8
HPPH.sln
@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Reference", "HPPH.Refe
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Generators", "HPPH.Generators\HPPH.Generators.csproj", "{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Generators", "HPPH.Generators\HPPH.Generators.csproj", "{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.SkiaSharp", "HPPH.SkiaSharp\HPPH.SkiaSharp.csproj", "{E70CD72D-1A41-413E-B0C6-24958733A773}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -45,6 +47,10 @@ Global
|
|||||||
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.Build.0 = Release|Any CPU
|
{16EC37E4-3EF1-47AF-B257-465334B36571}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E70CD72D-1A41-413E-B0C6-24958733A773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E70CD72D-1A41-413E-B0C6-24958733A773}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E70CD72D-1A41-413E-B0C6-24958733A773}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E70CD72D-1A41-413E-B0C6-24958733A773}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user