Added first extensions to work with System.Drawing-bitmaps

This commit is contained in:
DarthAffe committed 2024-07-11 00:24:12 +02:00
1 parent be39739f4d
commit 02da924713
4 files changed
+78 -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="System.Drawing.Common" Version="8.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HPPH\HPPH.csproj" />
</ItemGroup>
</Project>
+32
View File
@@ -0,0 +1,32 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.Versioning;
namespace HPPH.System.Drawing;
public static class ImageExtension
{
public static Bitmap ToBitmap(this IImage image)
{
throw new NotImplementedException();
}
[SupportedOSPlatform("windows")]
public static unsafe IImage ToImage(this Bitmap bitmap)
{
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
ReadOnlySpan<byte> buffer = new(data.Scan0.ToPointer(), data.Stride * data.Height);
IImage image;
if (data.PixelFormat.HasFlag(PixelFormat.Format24bppRgb))
image = Image<ColorRGB>.Create(buffer, data.Width, data.Height, data.Stride);
else if (data.PixelFormat.HasFlag(PixelFormat.Format32bppArgb))
image = Image<ColorARGB>.Create(buffer, data.Width, data.Height, data.Stride);
else throw new NotSupportedException($"Unsupported pixel format '{bitmap.PixelFormat}'.");
bitmap.UnlockBits(data);
return image;
}
}
+21
View File
@@ -0,0 +1,21 @@
using System.Drawing;
using System.Runtime.Versioning;
namespace HPPH.System.Drawing;
public static class ImageHelper
{
[SupportedOSPlatform("windows")]
public static IImage LoadImage(string path)
{
using Bitmap bmp = new(path);
return bmp.ToImage();
}
[SupportedOSPlatform("windows")]
public static IImage LoadImage(Stream stream)
{
using Bitmap bmp = new(stream);
return bmp.ToImage();
}
}