Skip to content

Commit 907e7a9

Browse files
committed
Add Magick.NET demo
1 parent 9f064a0 commit 907e7a9

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Net.Codecrete.QrCodeGenerator.Demo</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="11.3.0" />
13+
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.0.3" />
14+
</ItemGroup>
15+
16+
</Project>

Demo-ImageMagick/Demo-ImageMagick.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32630.192
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-ImageMagick", "Demo-ImageMagick.csproj", "{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BC2EF2FB-30F7-431B-B92C-05E9C7B7B07B}
24+
EndGlobalSection
25+
EndGlobal

Demo-ImageMagick/Program.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// QR code generator library (.NET)
3+
// https://github.com/manuelbl/QrCodeGenerator
4+
//
5+
// Copyright (c) 2022 suxiaobu9, Manuel Bleichenbacher
6+
// Licensed under MIT License
7+
// https://opensource.org/licenses/MIT
8+
//
9+
10+
using ImageMagick;
11+
using Net.Codecrete.QrCodeGenerator;
12+
13+
string text = "Hello, world!",
14+
fileName = "hello-world-QR.png";
15+
16+
var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium);
17+
18+
qr.SaveAsPng(fileName, 10, 4, MagickColors.Black, MagickColors.White);
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// QR code generator library (.NET)
3+
// https://github.com/manuelbl/QrCodeGenerator
4+
//
5+
// Copyright (c) 2022 suxiaobu9, Manuel Bleichenbacher
6+
// Licensed under MIT License
7+
// https://opensource.org/licenses/MIT
8+
//
9+
10+
using ImageMagick;
11+
12+
namespace Net.Codecrete.QrCodeGenerator;
13+
14+
public static class QrCodeImageExtensions
15+
{
16+
/// <summary>
17+
/// Creates a image of this QR code.
18+
/// <para>
19+
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
20+
/// equivalent to the width and height of each QR code module. Additionally, the number
21+
/// of modules to add as a border to all four sides can be specified.
22+
/// </para>
23+
/// <para>
24+
/// For example, <c>ToBitmap(scale: 10, border: 4)</c> means to pad the QR code with 4 white
25+
/// border modules on all four sides, and use 10&#xD7;10 pixels to represent each module.
26+
/// </para>
27+
/// </summary>
28+
/// <param name="scale">The width and height, in pixels, of each module.</param>
29+
/// <param name="border">The number of border modules to add to each of the four sides.</param>
30+
/// <param name="background">The background color.</param>
31+
/// <param name="foreground">The foreground color.</param>
32+
/// <returns></returns>
33+
/// <exception cref="ArgumentOutOfRangeException"></exception>
34+
public static MagickImage ToImage(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background)
35+
{
36+
if (scale <= 0)
37+
{
38+
throw new ArgumentOutOfRangeException(nameof(scale), " Value out of range");
39+
}
40+
41+
if (border < 0)
42+
{
43+
throw new ArgumentOutOfRangeException(nameof(border), " Value out of range");
44+
}
45+
46+
var size = qrCode.Size;
47+
var dim = (size + border * 2) * scale;
48+
49+
if (dim > short.MaxValue)
50+
{
51+
throw new ArgumentOutOfRangeException(nameof(scale), " Scale or border too large");
52+
}
53+
54+
var image = new MagickImage(background, dim, dim)
55+
{
56+
Format = MagickFormat.Png,
57+
};
58+
59+
var drawables = new Drawables();
60+
drawables.FillColor(foreground);
61+
62+
for (var x = 0; x < size; x++)
63+
{
64+
var pointerX = (x + border) * scale;
65+
66+
for (var y = 0; y < size; y++)
67+
{
68+
if (qrCode.GetModule(x, y))
69+
{
70+
var pointerY = (y + border) * scale;
71+
drawables.Rectangle(pointerX, pointerY, pointerX + scale - 1, pointerY + scale - 1);
72+
}
73+
}
74+
}
75+
drawables.Draw(image);
76+
return image;
77+
}
78+
79+
/// <summary>
80+
/// Creates a PNG image of this QR code and returns it as a byte array.
81+
/// <para>
82+
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
83+
/// equivalent to the width and height of each QR code module. Additionally, the number
84+
/// of modules to add as a border to all four sides can be specified.
85+
/// </para>
86+
/// <para>
87+
/// For example, <c>ToPng(scale: 10, border: 4)</c> means to pad the QR code with 4 white
88+
/// border modules on all four sides, and use 10&#xD7;10 pixels to represent each module.
89+
/// </para>
90+
/// </summary>
91+
/// <param name="scale">The width and height, in pixels, of each module.</param>
92+
/// <param name="border">The number of border modules to add to each of the four sides.</param>
93+
/// <param name="foreground">The foreground color.</param>
94+
/// <param name="background">The background color.</param>
95+
/// <returns></returns>
96+
public static byte[] ToPng(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background)
97+
{
98+
using var image = qrCode.ToImage(scale, border, foreground, background);
99+
return image.ToByteArray();
100+
}
101+
102+
/// <summary>
103+
/// Saves this QR code as a PNG file.
104+
/// <para>
105+
/// The <paramref name="scale"/> parameter specifies the scale of the image, which is
106+
/// equivalent to the width and height of each QR code module. Additionally, the number
107+
/// of modules to add as a border to all four sides can be specified.
108+
/// </para>
109+
/// <para>
110+
/// For example, <c>SaveAsPng("qrcode.png", scale: 10, border: 4)</c> means to pad the QR code with 4 white
111+
/// border modules on all four sides, and use 10&#xD7;10 pixels to represent each module.
112+
/// </para>
113+
/// </summary>
114+
/// <param name="scale">The width and height, in pixels, of each module.</param>
115+
/// <param name="border">The number of border modules to add to each of the four sides.</param>
116+
/// <param name="foreground">The foreground color.</param>
117+
/// <param name="background">The background color.</param>
118+
public static void SaveAsPng(this QrCode qrCode, string fileName, int scale, int border, MagickColor foreground, MagickColor background)
119+
{
120+
using var image = qrCode.ToImage(scale, border, foreground, background);
121+
image.Write(fileName);
122+
}
123+
}

Demo-ImageMagick/README.me

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Saving as PNG using Magick.NET
2+
3+
This example program shows how to create a QR code and save it as a PNG file using the [Magick.NET](https://github.com/dlemstra/Magick.NET) image manipulation library (based on ImageMagick).

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ Several example projects are provided:
166166
- [Demo-SkiaSharp](Demo-SkiaSharp): Demonstrates how a QR code can be saved a PNG file, using the SkiaSharp multi-platform raster image library.
167167

168168
- [Demo-ImageSharp](Demo-ImageSharp): Demonstrates how a QR code can be saved a PNG file, using the ImageSharp raster image library. Additionally, a QR code with an image in the center is created.
169+
170+
- [Demo-SkiaSharp](Demo-ImageMagick): Demonstrates how a QR code can be saved a PNG file, using the Magick.NET image manipulation library (based on ImageMagick).

0 commit comments

Comments
 (0)