Skip to content

Commit

Permalink
QR code with image
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Dec 30, 2021
1 parent 46e7901 commit 7e734e6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Demo-ImageSharp/Demo-ImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
<Company>Codecrete</Company>
</PropertyGroup>

<ItemGroup>
<None Remove="heart.png" />
</ItemGroup>

<ItemGroup>
<Content Include="heart.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.*" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta13" />
Expand Down
36 changes: 36 additions & 0 deletions Demo-ImageSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// https://opensource.org/licenses/MIT
//

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System;
using System.IO;

Expand All @@ -16,6 +18,13 @@ internal class Program
{
// Create a QR code and save it as a PNG.
internal static void Main()
{
HelloWorld();
QrCodeWithImage();
}


internal static void HelloWorld()
{
var text = "Hello, world!";
var filename = "hello-world-QR.png";
Expand All @@ -25,5 +34,32 @@ internal static void Main()

Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}");
}

internal static void QrCodeWithImage()
{
var text = "https://github.com/manuelbl/QrCodeGenerator";
var filename = "qr-code-with-image.png";
var logoFilename = "heart.png";
const float logoWidth = 0.15f; // logo will have 15% the width of the QR code

var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium);

using (var bitmap = qr.ToBitmap(scale: 10, border: 4))
using (var logo = Image.Load(logoFilename))
{
// resize logo
var w = (int)Math.Round(bitmap.Width * logoWidth);
logo.Mutate(logo => logo.Resize(w, 0));

// draw logo in center
var topLeft = new Point((bitmap.Width - logo.Width) / 2, (bitmap.Height - logo.Height) / 2);
bitmap.Mutate(img => img.DrawImage(logo, topLeft, 1));

// save as PNG
bitmap.SaveAsPng(filename);
}

Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}");
}
}
}
2 changes: 2 additions & 0 deletions Demo-ImageSharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

This example program shows how to create a QR code and save it as a PNG file using the [ImageSharp](https://github.com/SixLabors/ImageSharp) rasterization library.

Additionally, it demonstrates how to add an image in the center of the QR code.

The use of ImageSharp is recommended if the project already uses ImageSharp.
Binary file added Demo-ImageSharp/heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,4 @@ Several example projects are provided:

- [Demo-SkiaSharp](Demo-SkiaSharp): Demonstrates how a QR code can be saved a PNG file, using the SkiaSharp multi-platform raster image library.

- [Demo-ImageSharp](Demo-ImageSharp): Demonstrates how a QR code can be saved a PNG file, using the ImageSharp raster image library.
- [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.

0 comments on commit 7e734e6

Please sign in to comment.