QR Code in C# asp.net
Loading
QR Code in C# asp.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Leon DRank #851 · 2.6K reputationPosted Sep 24, 2025, 11:15 PM EDT
For QR code generation, you can utilize Free Spire.Barcode. First, install it via NuGet:
Second, refer to the following code to generate a QR code:
using Spire.Barcode; using System.Drawing.Imaging; namespace QRGeneration { internal class Program { static void Main(string[] args) { // Configure the barcode settings for QR code generation var settings = new BarcodeSettings { // Set barcode type to QR Code Type = BarCodeType.QRCode, // Set the main data to encode into the QR code Data = "https://x.com/", Data2D = "https://x.com/", // Set error correction level to Medium QRCodeECL = QRCodeECL.M, // Set the width of the bar module X = 3, // Hide text (QR code data) ShowText = false }; // Create a BarCodeGenerator object using the defined settings var generator = new BarCodeGenerator(settings); // Generate the QR code image var qrCodeImage = generator.GenerateImage(); // Save the QR code image as a PNG file qrCodeImage.Save("simple_qr_code.png", ImageFormat.Png); } } }This library also allows you to personalize QR code, including adding logo and text labels, customizing border, backgroud color, image size and more.
References:
https://www.e-iceblue.com/Tutorials/NET/Spire.Barcode-for-.NET/Program-Guide/Create-and-Scan-Barcode/generate-qr-code-in-asp-net-csharp.html
https://medium.com/@alice.yang_10652/c-qr-code-generator-how-to-create-and-customize-qr-codes-full-guide-42bf9473e187
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 14, 2025, 3:08 AM EDT
Absolutely! Generating QR codes in C# ASP.NET is quite straightforward using libraries like QRCoder (very popular, open-source, and easy to use). Here’s a comprehensive guide for both Web Forms and MVC projects.
1. Install the QRCoder NuGet Package
Use NuGet Package Manager:
Or via the NuGet GUI in Visual Studio.
2. Generate QR Code in ASP.NET (Web Forms)
Example: Display QR Code in an Image control
ASPX Page:
Code-behind (C#):
using QRCoder; using System.Drawing; using System.IO; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string qrText = "https://www.example.com"; // Your URL or text using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q); using (QRCode qrCode = new QRCode(qrCodeData)) { using (Bitmap qrCodeImage = qrCode.GetGraphic(20)) { using (MemoryStream ms = new MemoryStream()) { qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); byte[] byteImage = ms.ToArray(); imgQRCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage); } } } } } }This generates a QR code dynamically and displays it in your ASP.NET page.
3. Generate QR Code in ASP.NET MVC
Controller Action:
using QRCoder; using System.Drawing; using System.IO; using System.Web.Mvc; public ActionResult GenerateQRCode(string text = "https://www.example.com") { using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.Q); using (QRCode qrCode = new QRCode(qrCodeData)) { using (Bitmap qrCodeImage = qrCode.GetGraphic(20)) { using (MemoryStream ms = new MemoryStream()) { qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png); return File(ms.ToArray(), "image/png"); } } } } }View:
This way, the QR code is generated dynamically and rendered as an image in the browser.
4. Customization Options (QRCoder)
Pixel size: Adjust the
GetGraphic()parameterColors:
GetGraphic(20, Color.Black, Color.White)for foreground/backgroundLogo in QR:
GetGraphic(20, Color.Black, Color.White, logoBitmap)Error correction level: L, M, Q, H (higher = more robust but denser)
5. Tips for ASP.NET Projects
Cache QR codes if generating repeatedly to improve performance.
For high-traffic websites, consider generating QR codes asynchronously.
Use PNG for better quality (avoid JPEG artifacts).