DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Trending

  • Your API Authentication Isn’t Broken; It’s Quietly Failing in These 6 Ways
  • Introduction to Retrieval Augmented Generation (RAG)
  • How Retry Storms Crash API-Led Systems: Bounded Reliability Patterns for Distributed Architectures
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  1. DZone
  2. Coding
  3. Frameworks
  4. QR Code Generator in ASP.NET Core Using Zxing.Net

QR Code Generator in ASP.NET Core Using Zxing.Net

If you're looking to start your own online store, learn how to create barcodes using ASP.NET Core and the third party library, Zxing.Net.

By 
Rajeesh Menoth user avatar
Rajeesh Menoth
·
May. 30, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
29.7K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will explain how to create a QR Code Generator in ASP.NET Core 1.0, using Zxing.Net.

Background

I tried to create a QR Code Generator in ASP.NET Core, using third party libraries. But in most cases, barcodes are not fully supported in ASP.NET Core because of some version issues, etc. I searched a lot in Google but finally, I found “Zxing.Net” - a library which supports the decoding and generating of barcodes. I had a discussion with MicJahn and came up with a great solution.

Zxing.Net

A library which supports the decoding and generating of the barcodes (Example: QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within the images.

Assemblies Required

The assemblies given below are required for QR Code Generator.

using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.IO;
using ZXing.QrCode;

Packages Required

We need the packages given below for drawing and creating a QR Code Generator.

"CoreCompat.System.Drawing": "1.0.0-beta006",    
"ZXing.Net": "0.15.0" 

C#

The QRCodeTagHelper class given below contains QR Code Generator methods, etc.

namespace QRCodeApp
{
[HtmlTargetElement("qrcode")]
public class QRCodeTagHelper : TagHelper
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var QrcodeContent = context.AllAttributes["content"].Value.ToString();
var alt = context.AllAttributes["alt"].Value.ToString();
var width = 250; // width of the Qr Code
var height = 250; // height of the Qr Code
var margin = 0;

var qrCodeWriter = new ZXing.BarcodeWriterPixelData
{
Format = ZXing.BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions { Height = height, Width = width, Margin = margin }
};

var pixelData = qrCodeWriter.Write(QrcodeContent);

// creating a bitmap from the raw pixel data; if only black and white colors are used it makes no difference
// that the pixel data ist BGRA oriented and the bitmap is initialized with RGB
using (var bitmap = new System.Drawing.Bitmap(pixelData.Width, pixelData.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
using (var ms = new MemoryStream())
{

var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, pixelData.Width, pixelData.Height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
try
{
// we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0,
pixelData.Pixels.Length);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
// save to stream as PNG
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

output.TagName = "img";
output.Attributes.Clear();
output.Attributes.Add("width", width);
output.Attributes.Add("height", height);
output.Attributes.Add("alt", alt);
output.Attributes.Add("src",
String.Format("data:image/png;base64,{0}", Convert.ToBase64String(ms.ToArray())));
}
}
}
}

Index.chtml

The code given below will display a QR Code Generator.

@{
    ViewData["Title"] = "Home";
}

<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3>

A library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.

<qrcode alt="QR Code" content="https://rajeeshmenoth.wordpress.com/" />
https://rajeeshmenoth.wordpress.com/

_ViewImports.cshtml

The code Injecting TagHelper given below is in the entire Application.

 @addTagHelper "*, QRCodeApp" 

project.json

The dependencies given below are required to create a QR Code Application.

{
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.1.2",
    "Microsoft.AspNetCore.Mvc.Core": "1.1.2",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.1.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "CoreCompat.System.Drawing": "1.0.0-beta006",
    "ZXing.Net": "0.15.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net452": { }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Output

QRCode Generator

Image title

References

  • Micjahn/Zxing.Net
  • Discussion
  • Download Source Code

See Also

You can download other ASP.NET Core 1.0 source codes from MSDN Code, using the links mentioned below.

  • Middleware And Static files In ASP.NET Core 1.0 – Part One
  • Middleware And Static files In ASP.NET Core 1.0 – Part Two
  • Create An Aurelia Single Page Application In ASP.NET Core 1.0
  • Create Rest API Or Web API With ASP.NET Core 1.0
  • Adding A Configuration Source File In ASP.NET Core 1.0
  • Code First Migration – ASP.NET Core MVC With EntityFrameWork Core
  • Send Email Using ASP.NET CORE 1.1 With MailKit In VisualStudio 2017

Conclusion

We learned how to create a QR Code Generator in ASP.NET Core 1.0 using Zxing.Net. I hope you liked this article. Please share your valuable suggestions and feedback.

ASP.NET ASP.NET Core QR code

Published at DZone with permission of Rajeesh Menoth. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GDPR Compliance With .NET: Securing Data the Right Way
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Developing Minimal APIs Quickly With Open Source ASP.NET Core
  • Revolutionizing Content Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook