Repository: manuelbl/QrCodeGenerator Branch: master Commit: d5d7336a2377 Files: 113 Total size: 1.0 MB Directory structure: gitextract_hpw2vdf4/ ├── .config/ │ └── dotnet-tools.json ├── .github/ │ └── workflows/ │ ├── continuous-integration.yml │ └── demos.yaml ├── .gitignore ├── Demo-ASP.NET-Core/ │ ├── Demo-ASP.NET-Core.csproj │ ├── Demo-ASP.NET-Core.sln │ ├── Program.cs │ ├── Properties/ │ │ └── launchSettings.json │ ├── QrCodeBitmapExtensions.cs │ ├── QrCodeController.cs │ ├── README.md │ ├── Startup.cs │ ├── appsettings.Development.json │ ├── appsettings.json │ └── wwwroot/ │ ├── home.html │ ├── styles.css │ └── ui.js ├── Demo-ImageMagick/ │ ├── Demo-ImageMagick.csproj │ ├── Demo-ImageMagick.sln │ ├── Program.cs │ ├── QrCodeImageExtensions.cs │ └── README.md ├── Demo-ImageSharp/ │ ├── Demo-ImageSharp.csproj │ ├── Demo-ImageSharp.sln │ ├── Program.cs │ ├── QrCodeBitmapExtensions.cs │ └── README.md ├── Demo-QRCode-Variety/ │ ├── Demo-QRCode-Variety.csproj │ ├── Demo-QRCode-Variety.sln │ ├── Program.cs │ └── README.md ├── Demo-SkiaSharp/ │ ├── Demo-SkiaSharp.csproj │ ├── Demo-SkiaSharp.sln │ ├── Program.cs │ ├── QrCodeBitmapExtensions.cs │ └── README.md ├── Demo-System-Drawing/ │ ├── Demo-System-Drawing.csproj │ ├── Demo-System-Drawing.sln │ ├── Program.cs │ ├── QrCodeBitmapExtensions.cs │ └── README.md ├── Demo-VCard/ │ ├── Program.cs │ ├── VCardDemo.csproj │ └── VCardDemo.sln ├── Demo-WinForms/ │ ├── Demo-WinForms.csproj │ ├── Demo-WinForms.sln │ ├── Form1.Designer.cs │ ├── Form1.cs │ ├── Form1.resx │ ├── Program.cs │ ├── QrCodeBitmapExtensions.cs │ ├── QrCodeControl.cs │ └── README.md ├── Demo-WinUI/ │ ├── Demo-WinUI/ │ │ ├── App.xaml │ │ ├── App.xaml.cs │ │ ├── Demo-WinUI.csproj │ │ ├── MainViewModel.cs │ │ ├── MainWindow.xaml │ │ ├── MainWindow.xaml.cs │ │ ├── NativeMethods.txt │ │ ├── Package.appxmanifest │ │ ├── Properties/ │ │ │ ├── PublishProfiles/ │ │ │ │ ├── win-arm64.pubxml │ │ │ │ ├── win-x64.pubxml │ │ │ │ └── win-x86.pubxml │ │ │ └── launchSettings.json │ │ ├── QrCodeControl.xaml │ │ ├── QrCodeControl.xaml.cs │ │ ├── QrCodeDrawing.cs │ │ └── app.manifest │ ├── Demo-WinUI.sln │ └── README.md ├── Demo-WindowsPresentationFoundation/ │ ├── App.xaml │ ├── App.xaml.cs │ ├── AssemblyInfo.cs │ ├── Demo-WindowsPresentationFoundation.csproj │ ├── Demo-WindowsPresentationFoundation.sln │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ ├── QrCodeDrawing.cs │ ├── README.md │ └── app.manifest ├── LICENSE ├── QrCodeGenerator/ │ ├── BitArrayExtensions.cs │ ├── DataTooLongException.cs │ ├── Graphics.cs │ ├── Key.snk │ ├── Objects.cs │ ├── PngBuilder.cs │ ├── QrCode.cs │ ├── QrCodeGenerator.csproj │ ├── QrSegment.cs │ ├── QrSegmentAdvanced.cs │ ├── ReedSolomonGenerator.cs │ ├── docfx/ │ │ ├── api/ │ │ │ └── index.md │ │ ├── docfx.json │ │ └── index.md │ └── docs/ │ └── README.md ├── QrCodeGenerator.sln ├── QrCodeGeneratorTest/ │ ├── BitArrayExtensionsTest.cs │ ├── KanjiTest.cs │ ├── PngTest.cs │ ├── QrCodeBitmapTest.cs │ ├── QrCodeDataProvider.cs │ ├── QrCodeGeneratorTest.csproj │ ├── QrCodeTest.cs │ ├── QrCodeTestCase.cs │ ├── QrSegmentEncodingTest.cs │ ├── QrSegmentRegexTest.cs │ ├── RandomData.cs │ ├── StructuredAppendTest.cs │ ├── SvgTest.cs │ └── TestHelper.cs └── README.md ================================================ FILE CONTENTS ================================================ ================================================ FILE: .config/dotnet-tools.json ================================================ { "version": 1, "isRoot": true, "tools": { "dotnet-validate": { "version": "0.0.1-preview.304", "commands": [ "dotnet-validate" ] } } } ================================================ FILE: .github/workflows/continuous-integration.yml ================================================ name: Continuous Integration on: [push, pull_request] env: Configuration: Release ContinuousIntegrationBuild: true DOTNET_CLI_TELEMETRY_OPTOUT: true DOTNET_NOLOGO: true DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: true TERM: xterm-256color jobs: package: strategy: matrix: os: [ macos-latest, ubuntu-latest, windows-latest ] fail-fast: false runs-on: ${{ matrix.os }} name: Build and run tests steps: - name: Checkout git repository uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions/setup-dotnet@v4 with: dotnet-version: | 6.x 8.x - name: Restore NuGet packages run: dotnet restore - name: Build solution run: dotnet build --no-restore --verbosity normal - name: Run tests run: dotnet test --no-build --verbosity normal - name: Upload test results uses: actions/upload-artifact@v4 if: always() with: name: TestResults-${{ runner.os }} path: TestResults-*.html - name: Create and validate NuGet packages run: | dotnet tool restore dotnet pack --no-build --verbosity normal if: startsWith(matrix.os,'windows') ================================================ FILE: .github/workflows/demos.yaml ================================================ name: Demos on: [push, pull_request] env: Configuration: Release ContinuousIntegrationBuild: true DOTNET_CLI_TELEMETRY_OPTOUT: true DOTNET_NOLOGO: true DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: true TERM: xterm-256color jobs: build: runs-on: windows-latest name: Build demo projects steps: - name: Checkout git repository uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions/setup-dotnet@v4 with: dotnet-version: | 6.x 8.x - name: Create local NuGet source run: | mkdir ..\LocalPackages $pkgdir = Resolve-Path ..\LocalPackages dotnet nuget add source $pkgdir --name Local - name: Build library package run: | dotnet restore dotnet tool restore dotnet pack --verbosity normal - name: Install library package run: | $pkg = Resolve-Path QrCodeGenerator\bin\Release\Net.Codecrete.QrCodeGenerator.*.nupkg nuget push $pkg -Source Local - name: Build Demo-ImageSharp run: dotnet build working-directory: Demo-ImageSharp - name: Build Demo-ASP.NET-Core run: dotnet build working-directory: Demo-ASP.NET-Core - name: Build Demo-ImageMagick run: dotnet build working-directory: Demo-ImageMagick - name: Build Demo-QRCode-Variety run: dotnet build working-directory: Demo-QRCode-Variety - name: Build Demo-SkiaSharp run: dotnet build working-directory: Demo-SkiaSharp - name: Build Demo-System-Drawing run: dotnet build working-directory: Demo-System-Drawing - name: Build Demo-VCard run: dotnet build working-directory: Demo-VCard - name: Build Demo-WindowsPresentationFoundation run: dotnet build working-directory: Demo-WindowsPresentationFoundation - name: Build Demo-WinForms run: dotnet build working-directory: Demo-WinForms - name: Build Demo-WinUI run: dotnet build working-directory: Demo-WinUI ================================================ FILE: .gitignore ================================================ # User-specific files *.rsuser *.suo *.user *.userosscache *.sln.docstates # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs # Build results [Bb]in/ [Oo]bj/ # Visual Studio 2015/2017 cache/options directory .vs/ # Visual Studio Code .vscode/ # Rider .idea/ ================================================ FILE: Demo-ASP.NET-Core/Demo-ASP.NET-Core.csproj ================================================ net8.0 Net.Codecrete.QrCodeGenerator.Demo ================================================ FILE: Demo-ASP.NET-Core/Demo-ASP.NET-Core.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31919.166 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-ASP.NET-Core", "Demo-ASP.NET-Core.csproj", "{8EA82CD7-A88E-4A93-94B0-FA0C7DD1C7F1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {8EA82CD7-A88E-4A93-94B0-FA0C7DD1C7F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8EA82CD7-A88E-4A93-94B0-FA0C7DD1C7F1}.Debug|Any CPU.Build.0 = Debug|Any CPU {8EA82CD7-A88E-4A93-94B0-FA0C7DD1C7F1}.Release|Any CPU.ActiveCfg = Release|Any CPU {8EA82CD7-A88E-4A93-94B0-FA0C7DD1C7F1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {2902DABB-C256-46C5-AF46-35600F5F7A0E} EndGlobalSection EndGlobal ================================================ FILE: Demo-ASP.NET-Core/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace Net.Codecrete.QrCodeGenerator.Demo { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } } ================================================ FILE: Demo-ASP.NET-Core/Properties/launchSettings.json ================================================ { "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:25726", "sslPort": 44337 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "Demo_ASP.NET_Core": { "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } } ================================================ FILE: Demo-ASP.NET-Core/QrCodeBitmapExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using SkiaSharp; using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator { public static class QrCodeBitmapExtensions { /// /// The background color. /// The foreground color. public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) { // check arguments if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); } int size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap SKBitmap bitmap = new SKBitmap(dim, dim, SKColorType.Rgb888x, SKAlphaType.Opaque); using (SKCanvas canvas = new SKCanvas(bitmap)) { // draw background using (SKPaint paint = new SKPaint { Color = background }) { canvas.DrawRect(0, 0, dim, dim, paint); } // draw modules using (SKPaint paint = new SKPaint { Color = foreground }) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); } } } } } return bitmap; } /// /// Creates a bitmap (raster image) of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The resulting bitmap uses the pixel format . /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border) { return qrCode.ToBitmap(scale, border, SKColors.Black, SKColors.White); } /// /// The background color. /// The foreground color. public static byte[] ToPng(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) { using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); return data.ToArray(); } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static byte[] ToPng(this QrCode qrCode, int scale, int border) { return qrCode.ToPng(scale, border, SKColors.Black, SKColors.White); } /// /// The background color. /// The foreground color. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, SKColor foreground, SKColor background) { using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); using FileStream stream = File.OpenWrite(filename); data.SaveTo(stream); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) { qrCode.SaveAsPng(filename, scale, border, SKColors.Black, SKColors.White); } } } ================================================ FILE: Demo-ASP.NET-Core/QrCodeController.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using Microsoft.AspNetCore.Mvc; using System; using System.Text; namespace Net.Codecrete.QrCodeGenerator.Demo { /// /// Controller for generating QR code as PNG or SVG images /// [ApiController] public class QrCodeController : ControllerBase { private static readonly QrCode.Ecc[] errorCorrectionLevels = { QrCode.Ecc.Low, QrCode.Ecc.Medium, QrCode.Ecc.Quartile, QrCode.Ecc.High }; /// /// Generates QR code as PNG image /// /// Text to encode in QR code /// Error correction level (0: low ... 3: high) /// Border width in multiples of a module (QR code pixel) /// PNG image [HttpGet("qrcode/png")] [ResponseCache(Duration = 2592000)] public ActionResult GeneratePng([FromQuery(Name = "text")] string text, [FromQuery(Name = "ecc")] int? ecc, [FromQuery(Name = "border")] int? borderWidth) { ecc = Math.Clamp(ecc ?? 1, 0, 3); borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999); var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]); byte[] png = qrCode.ToPng(20, (int)borderWidth); return new FileContentResult(png, "image/png"); } /// /// Generates QR code as SVG image /// /// Text to encode in QR code /// Error correction level (0: low ... 3: high) /// Border width in multiples of a module (QR code pixel) /// SVG image [HttpGet("qrcode/svg")] [ResponseCache(Duration = 2592000)] public ActionResult GenerateSvg([FromQuery(Name = "text")] string text, [FromQuery(Name = "ecc")] int? ecc, [FromQuery(Name = "border")] int? borderWidth) { ecc = Math.Clamp(ecc ?? 1, 0, 3); borderWidth = Math.Clamp(borderWidth ?? 3, 0, 999999); var qrCode = QrCode.EncodeText(text, errorCorrectionLevels[(int)ecc]); byte[] svg = Encoding.UTF8.GetBytes(qrCode.ToSvgString((int)borderWidth)); return new FileContentResult(svg, "image/svg+xml; charset=utf-8"); } } } ================================================ FILE: Demo-ASP.NET-Core/README.md ================================================ # Sample code for ASP.NET Core This example program shows how to create a QR codes in an ASP.NET core application. The [`QrCodeController`](QrCodeController.cs) class receives the QR code text, border width and error correction level as query parameters and generates the QR code, either as an SVG or PNG. For PNG generation, the [SkiaSharp](https://github.com/mono/SkiaSharp) rasterization library is used. ================================================ FILE: Demo-ASP.NET-Core/Startup.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Rewrite; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Net.Codecrete.QrCodeGenerator.Demo { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } var rewriter = new RewriteOptions(); rewriter.AddRewrite("^$", "home.html", skipRemainingRules: false); app.UseRewriter(rewriter); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } } ================================================ FILE: Demo-ASP.NET-Core/appsettings.Development.json ================================================ { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } } } ================================================ FILE: Demo-ASP.NET-Core/appsettings.json ================================================ { "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*" } ================================================ FILE: Demo-ASP.NET-Core/wwwroot/home.html ================================================  QR Code

QR Code

================================================ FILE: Demo-ASP.NET-Core/wwwroot/styles.css ================================================ .content { max-width: 768px; margin-left: auto; margin-right: auto; padding-left: 1em; padding-right: 1em; background: #eee; } #qrcode { width: 80%; max-width: 400px; margin-top: 1em; margin-bottom: 2em; margin-left: auto; margin-right: auto; display: block; } #text { width: 90%; } .more-space { margin-top: 0.5em; margin-bottom: 0.5em; } h1 { letter-spacing: 0; text-align: center; width: 100%; } ================================================ FILE: Demo-ASP.NET-Core/wwwroot/ui.js ================================================ (function () { var qrCodeImage; var textField; var borderField; var eccSelect; function updateQrCode() { var url = new URL('qrcode/svg', document.baseURI); url.searchParams.append('text', textField.value); url.searchParams.append('ecc', eccSelect.value); url.searchParams.append('border', borderField.value); qrCodeImage.src = url; } function init() { textField = document.getElementById('text'); qrCodeImage = document.getElementById('qrcode'); borderField = document.getElementById('border'); eccSelect = document.getElementById('ecc'); textField.onchange = function () { updateQrCode(); } textField.oninput = function () { updateQrCode(); } borderField.onchange = function () { updateQrCode(); } borderField.oninput = function () { updateQrCode(); } eccSelect.onchange = function () { updateQrCode(); } } init(); })(); ================================================ FILE: Demo-ImageMagick/Demo-ImageMagick.csproj ================================================ Exe net8.0 Net.Codecrete.QrCodeGenerator.Demo enable enable ================================================ FILE: Demo-ImageMagick/Demo-ImageMagick.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.2.32630.192 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-ImageMagick", "Demo-ImageMagick.csproj", "{E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Debug|Any CPU.Build.0 = Debug|Any CPU {E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Release|Any CPU.ActiveCfg = Release|Any CPU {E4F86D1E-F08C-4BFE-BC37-9BED8A411E88}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BC2EF2FB-30F7-431B-B92C-05E9C7B7B07B} EndGlobalSection EndGlobal ================================================ FILE: Demo-ImageMagick/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2022 suxiaobu9, Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using ImageMagick; using Net.Codecrete.QrCodeGenerator; string text = "Hello, world!", fileName = "hello-world-QR.png"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); qr.SaveAsPng(fileName, 10, 4, MagickColors.Black, MagickColors.White); ================================================ FILE: Demo-ImageMagick/QrCodeImageExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2022 suxiaobu9, Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using ImageMagick; using ImageMagick.Drawing; namespace Net.Codecrete.QrCodeGenerator; public static class QrCodeImageExtensions { /// /// Creates a image of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The background color. /// The foreground color. /// /// public static MagickImage ToImage(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background) { if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), " Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), " Value out of range"); } var size = qrCode.Size; var dim = (uint)((size + border * 2) * scale); if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), " Scale or border too large"); } var image = new MagickImage(background, dim, dim) { Format = MagickFormat.Png, }; var drawables = new Drawables(); drawables.FillColor(foreground); for (var x = 0; x < size; x++) { var pointerX = (x + border) * scale; for (var y = 0; y < size; y++) { if (qrCode.GetModule(x, y)) { var pointerY = (y + border) * scale; drawables.Rectangle(pointerX, pointerY, pointerX + scale - 1, pointerY + scale - 1); } } } drawables.Draw(image); return image; } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The foreground color. /// The background color. /// public static byte[] ToPng(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background) { using var image = qrCode.ToImage(scale, border, foreground, background); return image.ToByteArray(); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The foreground color. /// The background color. public static void SaveAsPng(this QrCode qrCode, string fileName, int scale, int border, MagickColor foreground, MagickColor background) { using var image = qrCode.ToImage(scale, border, foreground, background); image.Write(fileName); } } ================================================ FILE: Demo-ImageMagick/README.md ================================================ # Saving as PNG using Magick.NET 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). ================================================ FILE: Demo-ImageSharp/Demo-ImageSharp.csproj ================================================ Exe net8.0 Net.Codecrete.QrCodeGenerator.Demo Net.Codecrete.QrCodeGenerator.Demo 2.1.0 Manuel Bleichenbacher, Project Nayuki QR Code Generator for .NET Demo application for QR Code Generation Copyright Manuel Bleichenbacher and Project Nayuki (MIT License) https://opensource.org/licenses/MIT https://github.com/manuelbl/QrCodeGenerator https://github.com/manuelbl/QrCodeGenerator Codecrete PreserveNewest ================================================ FILE: Demo-ImageSharp/Demo-ImageSharp.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30611.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo-ImageSharp", "Demo-ImageSharp.csproj", "{BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.ActiveCfg = Release|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94BC47F9-3AE8-471A-AC3F-CDEFA1B21089} EndGlobalSection EndGlobal ================================================ FILE: Demo-ImageSharp/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator.Demo { 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"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); // Create the QR code symbol qr.SaveAsPng(filename, scale: 10, border: 4); 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)}"); } } } ================================================ FILE: Demo-ImageSharp/QrCodeBitmapExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using SixLabors.ImageSharp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator { public static class QrCodeBitmapExtensions { /// /// The background color. /// The foreground color. public static Image ToBitmap(this QrCode qrCode, int scale, int border, Color foreground, Color background) { // check arguments if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); } int size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap var image = new Image(dim, dim); image.Mutate(img => { // draw background img.Fill(background); // draw modules for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { img.Fill(foreground, new Rectangle((x + border) * scale, (y + border) * scale, scale, scale)); } } } }); return image; } /// /// Creates a bitmap (raster image) of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The resulting bitmap uses the pixel format . /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static Image ToBitmap(this QrCode qrCode, int scale, int border) { return qrCode.ToBitmap(scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static byte[] ToPng(this QrCode qrCode, int scale, int border, Color foreground, Color background) { using var image = qrCode.ToBitmap(scale, border, foreground, background); using var ms = new MemoryStream(); image.SaveAsPng(ms); return ms.ToArray(); } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static byte[] ToPng(this QrCode qrCode, int scale, int border) { return qrCode.ToPng(scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, Color foreground, Color background) { using Image image = qrCode.ToBitmap(scale, border, foreground, background); image.SaveAsPng(filename); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) { qrCode.SaveAsPng(filename, scale, border, Color.Black, Color.White); } } } ================================================ FILE: Demo-ImageSharp/README.md ================================================ # Saving as PNG using ImageSharp 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. ================================================ FILE: Demo-QRCode-Variety/Demo-QRCode-Variety.csproj ================================================ Exe net8.0 Demo_Basic ================================================ FILE: Demo-QRCode-Variety/Demo-QRCode-Variety.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-QRCode-Variety", "Demo-QRCode-Variety.csproj", "{CE334406-7A4A-4455-889B-200DEBB6C08C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {CE334406-7A4A-4455-889B-200DEBB6C08C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CE334406-7A4A-4455-889B-200DEBB6C08C}.Debug|Any CPU.Build.0 = Debug|Any CPU {CE334406-7A4A-4455-889B-200DEBB6C08C}.Release|Any CPU.ActiveCfg = Release|Any CPU {CE334406-7A4A-4455-889B-200DEBB6C08C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {BB8EAC9D-6D83-4A9C-A867-300B9DD2B793} EndGlobalSection EndGlobal ================================================ FILE: Demo-QRCode-Variety/Program.cs ================================================ /* * QR code generator library (.NET) * * Copyright (c) Manuel Bleichenbacher (MIT License) * https://github.com/manuelbl/QrCodeGenerator * Copyright (c) Project Nayuki (MIT License) * https://www.nayuki.io/page/qr-code-generator-library * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. */ using System.Collections.Generic; using System.IO; using System.Text; namespace Net.Codecrete.QrCodeGenerator.Demo { internal class Program { // The main application program. internal static void Main() { DoBasicDemo(); DoVarietyDemo(); DoSegmentDemo(); DoMaskDemo(); DoBinaryDemo(); } #region Demo suite // Creates a single QR code, then writes it to an SVG file. private static void DoBasicDemo() { const string text = "Hello, world!"; // User-supplied Unicode text var errCorLvl = QrCode.Ecc.Low; // Error correction level var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR code symbol SaveAsSvg(qr, "hello-world-QR.svg", border: 4); // Save as SVG } // Creates a variety of QR codes that exercise different features of the library, and writes each one to file. private static void DoVarietyDemo() { // Numeric mode encoding (3.33 bits per digit) var qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.Medium); SaveAsSvg(qr, "pi-digits-QR.svg"); // Alphanumeric mode encoding (5.5 bits per character) qr = QrCode.EncodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.High); SaveAsSvg(qr, "alphanumeric-QR.svg", 2); // Unicode text as UTF-8 qr = QrCode.EncodeText("こんにちwa、世界! αβγδ", QrCode.Ecc.Quartile); SaveAsSvg(qr, "unicode-QR.svg", 3); // Moderately large QR code using longer text (from Lewis Carroll's Alice in Wonderland) qr = QrCode.EncodeText( "Alice was beginning to get very tired of sitting by her sister on the bank, " + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " + "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.High); SaveAsSvg(qr, "alice-wonderland-QR.svg", 10); } // Creates QR codes with manually specified segments for better compactness. private static void DoSegmentDemo() { // Illustration "silver" const string silver0 = "THE SQUARE ROOT OF 2 IS 1."; const string silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; var qr = QrCode.EncodeText(silver0 + silver1, QrCode.Ecc.Low); SaveAsSvg(qr, "sqrt2-monolithic-QR.svg", 3); var segs = new List { QrSegment.MakeAlphanumeric(silver0), QrSegment.MakeNumeric(silver1) }; qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); SaveAsSvg(qr, "sqrt2-segmented-QR.svg", 3); // Illustration "golden" const string golden0 = "Golden ratio φ = 1."; const string golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; const string golden2 = "......"; qr = QrCode.EncodeText(golden0 + golden1 + golden2, QrCode.Ecc.Low); SaveAsSvg(qr, "phi-monolithic-QR.svg", 5); segs = new List { QrSegment.MakeBytes(Encoding.UTF8.GetBytes(golden0)), QrSegment.MakeNumeric(golden1), QrSegment.MakeAlphanumeric(golden2) }; qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); SaveAsSvg(qr, "phi-segmented-QR.svg", 5); // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters const string madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; qr = QrCode.EncodeText(madoka, QrCode.Ecc.Low); SaveAsSvg(qr, "madoka-utf8-QR.svg", 4); segs = new List { QrSegmentAdvanced.MakeKanji(madoka) }; qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); SaveAsSvg(qr, "madoka-kanji-QR.svg", 4); } // Creates QR codes with the same size and contents but different mask patterns. private static void DoMaskDemo() { // Project Nayuki URL var segs = QrSegment.MakeSegments("https://www.nayuki.io/"); var qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High); SaveAsSvg(qr, "project-nayuki-automask-QR.svg", 6); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3); // Force mask 3 SaveAsSvg(qr, "project-nayuki-mask3-QR.svg", 6); // Chinese text as UTF-8 segs = QrSegment.MakeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 0); // Force mask 0 SaveAsSvg(qr, "unicode-mask0-QR.svg"); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 1); // Force mask 1 SaveAsSvg(qr, "unicode-mask1-QR.svg"); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 5); // Force mask 5 SaveAsSvg(qr, "unicode-mask5-QR.svg"); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 7); // Force mask 7 SaveAsSvg(qr, "unicode-mask7-QR.svg"); } private static void DoBinaryDemo() { // create binary data byte[] data = { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x01, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x0a, 0x00, 0x01, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, 0x01, 0x00, 0x3b }; var qr = QrCode.EncodeBinary(data, QrCode.Ecc.Medium); SaveAsSvg(qr, "binary.svg"); } #endregion private static void SaveAsSvg(QrCode qrCode, string filname, int border = 3) { string svg = qrCode.ToSvgString(border); // Convert to SVG XML code File.WriteAllText(filname, svg, Encoding.UTF8); // Write image to file } } } ================================================ FILE: Demo-QRCode-Variety/README.md ================================================ # Demonstration of various QR code This example program creates a series of QR code and saves them as SVG files. Aside from a simple standard case, the examples demonstrates the use of: - different encodings (numeric, alpha-numeric, Unicode) - different error correction levels - using multiple segments - using different masks - encoding binary data ================================================ FILE: Demo-SkiaSharp/Demo-SkiaSharp.csproj ================================================ Exe net8.0 Net.Codecrete.QrCodeGenerator.Demo Net.Codecrete.QrCodeGenerator.Demo 2.1.0 Manuel Bleichenbacher, Project Nayuki QR Code Generator for .NET Demo application for QR Code Generation Copyright Manuel Bleichenbacher and Project Nayuki (MIT License) https://opensource.org/licenses/MIT https://github.com/manuelbl/QrCodeGenerator https://github.com/manuelbl/QrCodeGenerator Codecrete ================================================ FILE: Demo-SkiaSharp/Demo-SkiaSharp.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30611.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo-SkiaSharp", "Demo-SkiaSharp.csproj", "{BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.ActiveCfg = Release|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94BC47F9-3AE8-471A-AC3F-CDEFA1B21089} EndGlobalSection EndGlobal ================================================ FILE: Demo-SkiaSharp/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator.Demo { internal class Program { // Create a QR code and save it as a PNG. internal static void Main() { var text = "Hello, world!"; var filename = "hello-world-QR.png"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); // Create the QR code symbol qr.SaveAsPng(filename, scale: 10, border: 4); Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}"); } } } ================================================ FILE: Demo-SkiaSharp/QrCodeBitmapExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using SkiaSharp; using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator { public static class QrCodeBitmapExtensions { /// /// The background color. /// The foreground color. public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) { // check arguments if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); } int size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap SKBitmap bitmap = new SKBitmap(dim, dim, SKColorType.Rgb888x, SKAlphaType.Opaque); using (SKCanvas canvas = new SKCanvas(bitmap)) { // draw background using (SKPaint paint = new SKPaint { Color = background }) { canvas.DrawRect(0, 0, dim, dim, paint); } // draw modules using (SKPaint paint = new SKPaint { Color = foreground }) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { canvas.DrawRect((x + border) * scale, (y + border) * scale, scale, scale, paint); } } } } } return bitmap; } /// /// Creates a bitmap (raster image) of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The resulting bitmap uses the pixel format . /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static SKBitmap ToBitmap(this QrCode qrCode, int scale, int border) { return qrCode.ToBitmap(scale, border, SKColors.Black, SKColors.White); } /// /// The background color. /// The foreground color. public static byte[] ToPng(this QrCode qrCode, int scale, int border, SKColor foreground, SKColor background) { using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); return data.ToArray(); } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static byte[] ToPng(this QrCode qrCode, int scale, int border) { return qrCode.ToPng(scale, border, SKColors.Black, SKColors.White); } /// /// The background color. /// The foreground color. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, SKColor foreground, SKColor background) { using SKBitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 90); using FileStream stream = File.OpenWrite(filename); data.SaveTo(stream); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) { qrCode.SaveAsPng(filename, scale, border, SKColors.Black, SKColors.White); } } } ================================================ FILE: Demo-SkiaSharp/README.md ================================================ # Saving as PNG using SkiaSharp This example program shows how to create a QR code and save it as a PNG file using the [SkiaSharp](https://github.com/mono/SkiaSharp) rasterization library. The use of SkiaSharp is recommended for multi-platform and all non-Windows projects. ================================================ FILE: Demo-System-Drawing/Demo-System-Drawing.csproj ================================================ Exe net8.0 Net.Codecrete.QrCodeGenerator.Demo Net.Codecrete.QrCodeGenerator.Demo 2.1.0 Manuel Bleichenbacher, Project Nayuki QR Code Generator for .NET Demo application for QR Code Generation Copyright Manuel Bleichenbacher and Project Nayuki (MIT License) https://opensource.org/licenses/MIT https://github.com/manuelbl/QrCodeGenerator https://github.com/manuelbl/QrCodeGenerator Codecrete ================================================ FILE: Demo-System-Drawing/Demo-System-Drawing.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30611.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo-System-Drawing", "Demo-System-Drawing.csproj", "{BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Debug|Any CPU.Build.0 = Debug|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.ActiveCfg = Release|Any CPU {BBB1A4A6-D82E-4233-8FC9-9300F9576FD2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {94BC47F9-3AE8-471A-AC3F-CDEFA1B21089} EndGlobalSection EndGlobal ================================================ FILE: Demo-System-Drawing/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.IO; namespace Net.Codecrete.QrCodeGenerator.Demo { internal class Program { // Create a QR code and save it as a PNG. internal static void Main() { var text = "Hello, world!"; var filename = "hello-world-QR.png"; var qr = QrCode.EncodeText(text, QrCode.Ecc.Medium); // Create the QR code symbol qr.SaveAsPng(filename, scale: 10, border: 4); Console.WriteLine($"The QR code has been saved as {Path.GetFullPath(filename)}"); } } } ================================================ FILE: Demo-System-Drawing/QrCodeBitmapExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace Net.Codecrete.QrCodeGenerator { /// /// QrCode extension for creating bitmaps using System.Drawing classes. /// /// In .NET 6 and later versions, this extension will only work on Windows. /// /// public static class QrCodeBitmapExtensions { /// /// The background color. /// The foreground color. public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border, Color foreground, Color background) { // check arguments if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); } int size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap Bitmap bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(bitmap)) { Draw(qrCode, g, scale, border, foreground, background); } return bitmap; } /// /// Creates a bitmap (raster image) of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The resulting bitmap uses the pixel format . /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border) { return qrCode.ToBitmap(scale, border, Color.Black, Color.White); } /// /// Draws this QR code into the specified graphics context. /// /// The QR code is drawn at offset (0, 0). Use /// to draw it at a different position. /// /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, Draw(graphics, scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// /// The graphics context to draw in. /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border) { Draw(qrCode, graphics, scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border, Color foreground, Color background) { if (scale <= 0 || border < 0) { return; } int size = qrCode.Size; float dim = (size + border * 2) * scale; // draw background if (background != Color.Transparent) { using SolidBrush brush = new SolidBrush(background); graphics.FillRectangle(brush, 0, 0, dim, dim); } // draw modules using (SolidBrush brush = new SolidBrush(foreground)) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale); } } } } } /// /// The background color. /// The foreground color. public static byte[] ToPng(this QrCode qrCode, int scale, int border, Color foreground, Color background) { using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); return ms.ToArray(); } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static byte[] ToPng(this QrCode qrCode, int scale, int border) { return qrCode.ToPng(scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, Color foreground, Color background) { using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); bitmap.Save(filename, ImageFormat.Png); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) { qrCode.SaveAsPng(filename, scale, border, Color.Black, Color.White); } } } ================================================ FILE: Demo-System-Drawing/README.md ================================================ # Saving as PNG using System.Drawing This example program shows how to create a QR code and save it as a PNG file using the *System.Drawing* classes. With .NET 6 and later, *System.Drawing* has become a Windows only technology. While *System.Drawing* is available on macOS and Linux on earlier .NET version, its use is only recommended for Windows. ================================================ FILE: Demo-VCard/Program.cs ================================================ /* * QR code generator library (.NET) * * Copyright (c) Manuel Bleichenbacher (MIT License) * https://github.com/manuelbl/QrCodeGenerator * * Demo creating a QR code containing a vCard. * */ using MixERP.Net.VCards; using MixERP.Net.VCards.Models; using MixERP.Net.VCards.Serializer; using MixERP.Net.VCards.Types; using Net.Codecrete.QrCodeGenerator; using System.Collections.Generic; using System.IO; namespace VCardDemo { class Program { static void Main() { var vcard = new VCard { Version = VCardVersion.V3, FirstName = "Robin", LastName = "Hood", Organization = "Sherwood Inc.", Addresses = new List
{ new Address { Type = AddressType.Work, Street = "The Major Oak", Locality = "Sherwood Forest", PostalCode = "NG21 9RN", Country = "United Kingdom", } }, Telephones = new List { new Telephone { Type = TelephoneType.Work, Number = "+441623677321" } }, Emails = new List { new Email { Type = EmailType.Smtp, EmailAddress = "robin.hood@sherwoodinc.co.uk" } } }; var qrCode = QrCode.EncodeText(vcard.Serialize(), QrCode.Ecc.Medium); File.WriteAllText("vcard-qrcode.svg", qrCode.ToSvgString(3)); } } } ================================================ FILE: Demo-VCard/VCardDemo.csproj ================================================ Exe net8.0 ================================================ FILE: Demo-VCard/VCardDemo.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31702.278 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VCardDemo", "VCardDemo.csproj", "{5DE9E10C-E608-4661-AAE9-ABCF46C0F2F5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {5DE9E10C-E608-4661-AAE9-ABCF46C0F2F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5DE9E10C-E608-4661-AAE9-ABCF46C0F2F5}.Debug|Any CPU.Build.0 = Debug|Any CPU {5DE9E10C-E608-4661-AAE9-ABCF46C0F2F5}.Release|Any CPU.ActiveCfg = Release|Any CPU {5DE9E10C-E608-4661-AAE9-ABCF46C0F2F5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7E8A810C-1062-422C-B511-9634C165EB47} EndGlobalSection EndGlobal ================================================ FILE: Demo-WinForms/Demo-WinForms.csproj ================================================  WinExe net8.0-windows Net.Codecrete.QrCodeGenerator.Demo True ================================================ FILE: Demo-WinForms/Demo-WinForms.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo-WinForms", "Demo-WinForms.csproj", "{A3AD57D8-ED64-40DB-8EB1-F4E1B8EF18FD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {A3AD57D8-ED64-40DB-8EB1-F4E1B8EF18FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A3AD57D8-ED64-40DB-8EB1-F4E1B8EF18FD}.Debug|Any CPU.Build.0 = Debug|Any CPU {A3AD57D8-ED64-40DB-8EB1-F4E1B8EF18FD}.Release|Any CPU.ActiveCfg = Release|Any CPU {A3AD57D8-ED64-40DB-8EB1-F4E1B8EF18FD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0CCE4B82-DC41-4CAA-9643-0130009B5A28} EndGlobalSection EndGlobal ================================================ FILE: Demo-WinForms/Form1.Designer.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // namespace Net.Codecrete.QrCodeGenerator.Demo { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.qrCodeControl = new Net.Codecrete.QrCodeGenerator.QrCodeControl(); this.qrCodeText = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.errorCorrectionCombo = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.copyButton = new System.Windows.Forms.Button(); this.borderNumericUpDown = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.borderNumericUpDown)).BeginInit(); this.SuspendLayout(); // // qrCodeControl // this.qrCodeControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.qrCodeControl.BinaryData = null; this.qrCodeControl.BorderWidth = 3; this.qrCodeControl.ErrorCorrection = 2; this.qrCodeControl.Location = new System.Drawing.Point(32, 32); this.qrCodeControl.Name = "qrCodeControl"; this.qrCodeControl.Size = new System.Drawing.Size(712, 344); this.qrCodeControl.TabIndex = 0; this.qrCodeControl.TextData = "Test"; // // qrCodeText // this.qrCodeText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.qrCodeText.Location = new System.Drawing.Point(32, 420); this.qrCodeText.Name = "qrCodeText"; this.qrCodeText.Size = new System.Drawing.Size(712, 39); this.qrCodeText.TabIndex = 1; this.qrCodeText.Text = "QR code text"; this.qrCodeText.TextChanged += new System.EventHandler(this.QrCodeText_TextChanged); // // label1 // this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(32, 488); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(187, 32); this.label1.TabIndex = 2; this.label1.Text = "Error Correction:"; // // errorCorrectionCombo // this.errorCorrectionCombo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.errorCorrectionCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.errorCorrectionCombo.FormattingEnabled = true; this.errorCorrectionCombo.Items.AddRange(new object[] { "Low", "Medium", "Quartile", "High"}); this.errorCorrectionCombo.Location = new System.Drawing.Point(225, 485); this.errorCorrectionCombo.Name = "errorCorrectionCombo"; this.errorCorrectionCombo.Size = new System.Drawing.Size(186, 40); this.errorCorrectionCombo.TabIndex = 3; this.errorCorrectionCombo.SelectedIndexChanged += new System.EventHandler(this.ErrorCorrectionCombo_SelectedIndexChanged); // // label2 // this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(496, 488); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(90, 32); this.label2.TabIndex = 4; this.label2.Text = "Border:"; // // copyButton // this.copyButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.copyButton.Location = new System.Drawing.Point(498, 568); this.copyButton.Name = "copyButton"; this.copyButton.Size = new System.Drawing.Size(246, 46); this.copyButton.TabIndex = 6; this.copyButton.Text = "Copy QR Code"; this.copyButton.UseVisualStyleBackColor = true; this.copyButton.Click += new System.EventHandler(this.CopyButton_Click); // // borderNumericUpDown // this.borderNumericUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.borderNumericUpDown.Location = new System.Drawing.Point(592, 486); this.borderNumericUpDown.Name = "borderNumericUpDown"; this.borderNumericUpDown.Size = new System.Drawing.Size(152, 39); this.borderNumericUpDown.TabIndex = 7; this.borderNumericUpDown.Value = new decimal(new int[] { 3, 0, 0, 0}); this.borderNumericUpDown.ValueChanged += new System.EventHandler(this.BorderNumericUpDown_ValueChanged); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(13F, 32F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(774, 649); this.Controls.Add(this.borderNumericUpDown); this.Controls.Add(this.copyButton); this.Controls.Add(this.label2); this.Controls.Add(this.errorCorrectionCombo); this.Controls.Add(this.label1); this.Controls.Add(this.qrCodeText); this.Controls.Add(this.qrCodeControl); this.Name = "Form1"; this.Text = "QR Code"; ((System.ComponentModel.ISupportInitialize)(this.borderNumericUpDown)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private QrCodeControl qrCodeControl; private System.Windows.Forms.TextBox qrCodeText; private System.Windows.Forms.Label label1; private System.Windows.Forms.ComboBox errorCorrectionCombo; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button copyButton; private System.Windows.Forms.NumericUpDown borderNumericUpDown; } } ================================================ FILE: Demo-WinForms/Form1.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.Windows.Forms; namespace Net.Codecrete.QrCodeGenerator.Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); errorCorrectionCombo.SelectedIndex = 1; qrCodeControl.TextData = qrCodeText.Text; qrCodeControl.ErrorCorrection = errorCorrectionCombo.SelectedIndex; qrCodeControl.BorderWidth = (int)borderNumericUpDown.Value; } private void QrCodeText_TextChanged(object sender, EventArgs e) { qrCodeControl.TextData = qrCodeText.Text; } private void ErrorCorrectionCombo_SelectedIndexChanged(object sender, EventArgs e) { qrCodeControl.ErrorCorrection = errorCorrectionCombo.SelectedIndex; } private void BorderNumericUpDown_ValueChanged(object sender, EventArgs e) { qrCodeControl.BorderWidth = (int)borderNumericUpDown.Value; } private void CopyButton_Click(object sender, EventArgs e) { qrCodeControl.CopyToClipboard(); } } } ================================================ FILE: Demo-WinForms/Form1.resx ================================================  text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ================================================ FILE: Demo-WinForms/Program.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.Windows.Forms; namespace Net.Codecrete.QrCodeGenerator.Demo { internal static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } ================================================ FILE: Demo-WinForms/QrCodeBitmapExtensions.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace Net.Codecrete.QrCodeGenerator { /// /// QrCode extension for creating bitmaps using System.Drawing classes. /// /// In .NET 6 and later versions, this extension will only work on Windows. /// /// public static class QrCodeBitmapExtensions { /// /// The background color. /// The foreground color. public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border, Color foreground, Color background) { // check arguments if (scale <= 0) { throw new ArgumentOutOfRangeException(nameof(scale), "Value out of range"); } if (border < 0) { throw new ArgumentOutOfRangeException(nameof(border), "Value out of range"); } int size = qrCode.Size; int dim = (size + border * 2) * scale; if (dim > short.MaxValue) { throw new ArgumentOutOfRangeException(nameof(scale), "Scale or border too large"); } // create bitmap Bitmap bitmap = new Bitmap(dim, dim, PixelFormat.Format24bppRgb); using (Graphics g = Graphics.FromImage(bitmap)) { Draw(qrCode, g, scale, border, foreground, background); } return bitmap; } /// /// Creates a bitmap (raster image) of this QR code. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToBitmap(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// The resulting bitmap uses the pixel format . /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static Bitmap ToBitmap(this QrCode qrCode, int scale, int border) { return qrCode.ToBitmap(scale, border, Color.Black, Color.White); } /// /// Draws this QR code into the specified graphics context. /// /// The QR code is drawn at offset (0, 0). Use /// to draw it at a different position. /// /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, Draw(graphics, scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// /// The graphics context to draw in. /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border) { Draw(qrCode, graphics, scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static void Draw(this QrCode qrCode, Graphics graphics, float scale, float border, Color foreground, Color background) { if (scale <= 0 || border < 0) { return; } int size = qrCode.Size; float dim = (size + border * 2) * scale; // draw background if (background != Color.Transparent) { using SolidBrush brush = new SolidBrush(background); graphics.FillRectangle(brush, 0, 0, dim, dim); } // draw modules using (SolidBrush brush = new SolidBrush(foreground)) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (qrCode.GetModule(x, y)) { graphics.FillRectangle(brush, (x + border) * scale, (y + border) * scale, scale, scale); } } } } } /// /// The background color. /// The foreground color. public static byte[] ToPng(this QrCode qrCode, int scale, int border, Color foreground, Color background) { using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); using MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Png); return ms.ToArray(); } /// /// Creates a PNG image of this QR code and returns it as a byte array. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, ToPng(scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// The created bitmap representing this QR code. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static byte[] ToPng(this QrCode qrCode, int scale, int border) { return qrCode.ToPng(scale, border, Color.Black, Color.White); } /// /// The background color. /// The foreground color. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border, Color foreground, Color background) { using Bitmap bitmap = qrCode.ToBitmap(scale, border, foreground, background); bitmap.Save(filename, ImageFormat.Png); } /// /// Saves this QR code as a PNG file. /// /// The parameter specifies the scale of the image, which is /// equivalent to the width and height of each QR code module. Additionally, the number /// of modules to add as a border to all four sides can be specified. /// /// /// For example, SaveAsPng("qrcode.png", scale: 10, border: 4) means to pad the QR code with 4 white /// border modules on all four sides, and use 10×10 pixels to represent each module. /// /// /// If not specified, the foreground color is black (0x000000) und the background color always white (0xFFFFFF). /// /// /// The width and height, in pixels, of each module. /// The number of border modules to add to each of the four sides. /// is 0 or negative, is negative /// or the resulting image is wider than 32,768 pixels. public static void SaveAsPng(this QrCode qrCode, string filename, int scale, int border) { qrCode.SaveAsPng(filename, scale, border, Color.Black, Color.White); } } } ================================================ FILE: Demo-WinForms/QrCodeControl.cs ================================================ // // QR code generator library (.NET) // https://github.com/manuelbl/QrCodeGenerator // // Copyright (c) 2021 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using System; using System.Drawing; using System.Windows.Forms; namespace Net.Codecrete.QrCodeGenerator { /// /// Custom control for displaying a QR code /// public class QrCodeControl : Control { private string _textData; private byte[] _binaryData; private int _errorCorrection; private int _borderWidth; public QrCodeControl() { _textData = "Test"; _errorCorrection = 2; _borderWidth = 3; ResizeRedraw = true; } public string TextData { get { if (_binaryData != null) { return "binary data"; } return _textData; } set { _textData = value; _binaryData = null; if (_textData == null) { _textData = ""; } Invalidate(); } } public byte[] BinaryData { get { return _binaryData; } set { _binaryData = value; _textData = null; if (_binaryData == null) { _binaryData = new byte[0]; } Invalidate(); } } public int ErrorCorrection { get { return _errorCorrection; } set { _errorCorrection = Math.Min(Math.Max(value, 0), 3); Invalidate(); } } public int BorderWidth { get { return _borderWidth; } set { _borderWidth = Math.Max(value, 0); Invalidate(); } } private static readonly QrCode.Ecc[] errorCorrectionLevels = { QrCode.Ecc.Low, QrCode.Ecc.Medium, QrCode.Ecc.Quartile, QrCode.Ecc.High }; /// /// Creates the QrCode instance with the current settings. /// /// private QrCode CreateQrCode() { QrCode qrCode; var ecc = errorCorrectionLevels[_errorCorrection]; if (_binaryData != null) { qrCode = QrCode.EncodeBinary(_binaryData, ecc); } else { qrCode = QrCode.EncodeText(_textData, ecc); } return qrCode; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); int size = Math.Min(Width, Height); var graphics = e.Graphics; var qrCode = CreateQrCode(); graphics.TranslateTransform((Width - size) / 2, (Height - size) / 2); qrCode.Draw(graphics, scale: size / (float)(qrCode.Size + 2 * _borderWidth), border: _borderWidth, foreground: Color.Black, background: Color.White); } /// /// Copy the QR code to the clipboard. /// /// The QR code is copied as a bitmap. It uses a scaling factor of 20 to /// prevent a blurry result from upscaling. /// /// public void CopyToClipboard() { DataObject dataObject = new DataObject(); var qrCode = CreateQrCode(); dataObject.SetData(DataFormats.Bitmap, qrCode.ToBitmap(20, _borderWidth)); Clipboard.SetDataObject(dataObject); } } } ================================================ FILE: Demo-WinForms/README.md ================================================ # Windows Forms example application This example application shows how to use the QR code library in a Windows Forms application: - `QrCodeControl` is a custom control that can be used in any `Form`. - The control also implements copying the QR code to the clipboard. ![Windows Forms QR Code](QrCodeWinForms.png) ================================================ FILE: Demo-WinUI/Demo-WinUI/App.xaml ================================================ ================================================ FILE: Demo-WinUI/Demo-WinUI/App.xaml.cs ================================================ // // Swiss QR Bill Generator for .NET // Copyright (c) 2022 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using System; using System.Runtime.InteropServices; using Windows.Graphics; using WinRT.Interop; using Windows.Win32; using Windows.Win32.Foundation; namespace Net.Codecrete.QrCodeGenerator.Demo; /// /// QR Code application. /// public partial class App : Application { /// /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// public App() { InitializeComponent(); } /// /// Invoked when the application is launched. /// /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs args) { m_window = new MainWindow { Title = "QR Code" }; // Set initial windows size. Resize(500, 600); m_window.Activate(); } private Window m_window; private void Resize(int x, int y) { var scalingFactor = GetDpiScalingFactor(); GetAppWindow().Resize(new SizeInt32((int)(x * scalingFactor), (int)(y * scalingFactor))); } private AppWindow GetAppWindow() { IntPtr hWnd = WindowNative.GetWindowHandle(m_window); WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); return AppWindow.GetFromWindowId(wndId); } private double GetDpiScalingFactor() { IntPtr hWnd = WindowNative.GetWindowHandle(m_window); var dpi = PInvoke.GetDpiForWindow((HWND)hWnd); return (float)dpi / 96; } } ================================================ FILE: Demo-WinUI/Demo-WinUI/Demo-WinUI.csproj ================================================  WinExe net8.0-windows10.0.19041.0 10.0.17763.0 Net.Codecrete.QrCodeGenerator.Demo app.manifest x86;x64;ARM64 win-x86;win-x64;win-arm64 win-$(Platform).pubxml true true QrCodeWinUIDemo False True False True Never 0 all runtime; build; native; contentfiles; analyzers; buildtransitive MSBuild:Compile true ================================================ FILE: Demo-WinUI/Demo-WinUI/MainViewModel.cs ================================================ // // Swiss QR Bill Generator for .NET // Copyright (c) 2022 Manuel Bleichenbacher // Licensed under MIT License // https://opensource.org/licenses/MIT // using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System; using System.Threading.Tasks; using Windows.ApplicationModel.DataTransfer; using Windows.Storage.Streams; namespace Net.Codecrete.QrCodeGenerator.Demo; /// /// View model for MainWindow /// public partial class MainViewModel : ObservableObject { /// /// QR code text /// [ObservableProperty] string text = "Hello, world!"; /// /// QR code error correction level /// [ObservableProperty] QrCode.Ecc errorCorrection = QrCode.Ecc.Medium; /// /// Width of border around QR code (in QR code pixels) /// [ObservableProperty] int borderWidth = 3; private readonly Tuple[] errorCorrectionLevels_ = { new Tuple("Low", QrCode.Ecc.Low), new Tuple("Medium", QrCode.Ecc.Medium), new Tuple("Quartile", QrCode.Ecc.Quartile), new Tuple("High", QrCode.Ecc.High) }; /// /// List of error correction levels /// public Tuple[] ErrorCorrectionLevels => errorCorrectionLevels_; /// /// Copy the QR code to the clipboard (as a PNG image). /// /// [RelayCommand] async Task CopyToClipboard() { var qrCode = QrCode.EncodeText(Text, ErrorCorrection); // Don't close stream; it won't work anymore var stream = new InMemoryRandomAccessStream(); await QrCodeDrawing.WriteAsPng(stream, qrCode, 20, BorderWidth); var dataPackage = new DataPackage(); dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream)); Clipboard.SetContent(dataPackage); } } ================================================ FILE: Demo-WinUI/Demo-WinUI/MainWindow.xaml ================================================ Error Correction: Border Width: