mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Added first extensions to work with System.Drawing-bitmaps
This commit is contained in:
parent
be39739f4d
commit
02da924713
18
HPPH.System.Drawing/HPPH.System.Drawing.csproj
Normal file
18
HPPH.System.Drawing/HPPH.System.Drawing.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="System.Drawing.Common" Version="8.0.6" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\HPPH\HPPH.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
32
HPPH.System.Drawing/ImageExtension.cs
Normal file
32
HPPH.System.Drawing/ImageExtension.cs
Normal 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
HPPH.System.Drawing/ImageHelper.cs
Normal file
21
HPPH.System.Drawing/ImageHelper.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
HPPH.sln
8
HPPH.sln
@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Benchmark", "HPPH.Benc
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Reference", "HPPH.Reference\HPPH.Reference.csproj", "{1675FE68-1F51-4202-9567-BB46215B2BBD}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HPPH.Reference", "HPPH.Reference\HPPH.Reference.csproj", "{1675FE68-1F51-4202-9567-BB46215B2BBD}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "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
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HPPH.System.Drawing", "HPPH.System.Drawing\HPPH.System.Drawing.csproj", "{16EC37E4-3EF1-47AF-B257-465334B36571}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -39,6 +41,10 @@ Global
|
|||||||
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.Build.0 = Release|Any CPU
|
{C247512B-E6D2-4591-8AFA-F2268F1AEAB2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{16EC37E4-3EF1-47AF-B257-465334B36571}.Debug|Any CPU.ActiveCfg = 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.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