-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added QrCodes.System.Drawing library.
- Loading branch information
Showing
12 changed files
with
463 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/libs/QrCodes.System.Drawing/QrCodes.System.Drawing.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net4.6.2;netstandard2.0;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks> | ||
<RootNamespace>QrCodes</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="NuGet"> | ||
<PackageId>Oscore.$(AssemblyName)</PackageId> | ||
<PackageTags>$(PackageTags);system;drawing</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\QrCodes\QrCodes.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' != 'net4.6.2' "> | ||
<PackageReference Include="System.Drawing.Common" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
80 changes: 80 additions & 0 deletions
80
src/libs/QrCodes.System.Drawing/Renderers/ImageExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Drawing; | ||
using System.Drawing.Imaging; | ||
|
||
namespace QrCodes.Renderers; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class ImageExtensions | ||
{ | ||
private static void Save( | ||
this Image image, | ||
Stream stream, | ||
FileFormat fileFormat, | ||
int quality = 100) | ||
{ | ||
image.Save( | ||
stream: stream, | ||
format: fileFormat switch | ||
{ | ||
FileFormat.Bmp => ImageFormat.Bmp, | ||
FileFormat.Gif => ImageFormat.Gif, | ||
FileFormat.Ico => throw new NotSupportedException("ICO is not supported by System.Drawing."), | ||
FileFormat.Jpeg => ImageFormat.Jpeg, | ||
FileFormat.Png => ImageFormat.Png, | ||
FileFormat.Wbmp => throw new NotSupportedException("Wbmp is not supported by System.Drawing."), | ||
FileFormat.Webp => throw new NotSupportedException("Webp is not supported by System.Drawing."), | ||
FileFormat.Pkm => throw new NotSupportedException("Pkm is not supported by System.Drawing."), | ||
FileFormat.Ktx => throw new NotSupportedException("Ktx is not supported by System.Drawing."), | ||
FileFormat.Astc => throw new NotSupportedException("Astc is not supported by System.Drawing."), | ||
FileFormat.Dng => throw new NotSupportedException("Dng is not supported by System.Drawing."), | ||
FileFormat.Heif => throw new NotSupportedException("Heif is not supported by System.Drawing."), | ||
FileFormat.Avif => throw new NotSupportedException("Avif is not supported by System.Drawing."), | ||
FileFormat.Pbm => throw new NotSupportedException("PBM is not supported by System.Drawing."), | ||
FileFormat.Tga => throw new NotSupportedException("TGA is not supported by System.Drawing."), | ||
FileFormat.Tiff => ImageFormat.Tiff, | ||
_ => throw new ArgumentOutOfRangeException(nameof(fileFormat), fileFormat, null) | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="image"></param> | ||
/// <param name="fileFormat"></param> | ||
/// <param name="quality"></param> | ||
/// <exception cref="ArgumentOutOfRangeException"></exception> | ||
public static Stream ToStream( | ||
this Image image, | ||
FileFormat fileFormat, | ||
int quality = 100) | ||
{ | ||
image = image ?? throw new ArgumentNullException(nameof(image)); | ||
|
||
var stream = new MemoryStream(); | ||
image.Save(stream, fileFormat, quality); | ||
|
||
return stream; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="image"></param> | ||
/// <param name="fileFormat"></param> | ||
/// <param name="quality"></param> | ||
/// <returns></returns> | ||
public static byte[] ToBytes( | ||
this Image image, | ||
FileFormat fileFormat, | ||
int quality = 100) | ||
{ | ||
image = image ?? throw new ArgumentNullException(nameof(image)); | ||
|
||
using var stream = new MemoryStream(); | ||
image.Save(stream, fileFormat, quality); | ||
|
||
return stream.ToArray(); | ||
} | ||
} |
Oops, something went wrong.