using IronBarCode;// You may add styling with color, logo images or branding:QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);// Save as PDFmyQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");// Also Save as HTMLmyQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
using IronBarCode;
// You may add styling with color, logo images or branding:
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode myQRCodeWithLogo = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen);
// Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf");
// Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html");
ImportsIronBarCode' You may add styling with color, logo images or branding:Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")Private myQRCodeWithLogo AsGeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)' Save as PDFmyQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")' Also Save as HTMLmyQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
Imports IronBarCode
' You may add styling with color, logo images or branding:
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private myQRCodeWithLogo As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
myQRCodeWithLogo.ChangeBarCodeColor(System.Drawing.Color.DarkGreen)
' Save as PDF
myQRCodeWithLogo.SaveAsPdf("MyQRWithLogo.pdf")
' Also Save as HTML
myQRCodeWithLogo.SaveAsHtmlFile("MyQRWithLogo.html")
How Can I Create a Simple QR Code in C#?
使用CreateQrCode方法,仅用一行代码生成QR码:
using IronBarCode;using IronSoftware.Drawing;using System;// Verifying QR CodesQRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");GeneratedBarcodeMyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);if (!MyVerifiedQR.Verify()){Console.WriteLine("\t LightBlue is not dark enough to be read accurately. Lets try DarkBlue");MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);}MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");// open the barcode html file in your default web browserSystem.Diagnostics.Process.Start("MyVerifiedQR.html");
using IronBarCode;
using IronSoftware.Drawing;
using System;
// Verifying QR Codes
QRCodeLogo qrCodeLogo = new QRCodeLogo("visual-studio-logo.png");
GeneratedBarcode MyVerifiedQR = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo);
MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue);
if (!MyVerifiedQR.Verify())
{
Console.WriteLine("\t LightBlue is not dark enough to be read accurately. Lets try DarkBlue");
MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue);
}
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html");
// open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html");
ImportsMicrosoft.VisualBasicImportsIronBarCodeImportsIronSoftware.DrawingImportsSystem' Verifying QR CodesPrivate qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")PrivateMyVerifiedQRAsGeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)IfNotMyVerifiedQR.Verify() ThenConsole.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately. Lets try DarkBlue")MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)End IfMyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")' open the barcode html file in your default web browserSystem.Diagnostics.Process.Start("MyVerifiedQR.html")
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
' Verifying QR Codes
Private qrCodeLogo As New QRCodeLogo("visual-studio-logo.png")
Private MyVerifiedQR As GeneratedBarcode = QRCodeWriter.CreateQrCodeWithLogo("https://ironsoftware.com/", qrCodeLogo)
MyVerifiedQR.ChangeBarCodeColor(System.Drawing.Color.LightBlue)
If Not MyVerifiedQR.Verify() Then
Console.WriteLine(vbTab & " LightBlue is not dark enough to be read accurately. Lets try DarkBlue")
MyVerifiedQR.ChangeBarCodeColor(Color.DarkBlue)
End If
MyVerifiedQR.SaveAsHtmlFile("MyVerifiedQR.html")
' open the barcode html file in your default web browser
System.Diagnostics.Process.Start("MyVerifiedQR.html")
using IronBarCode;using System;using System.Linq;// Create Some Binary Data - This example equally well for Byte[] and System.IO.Streambyte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");// WRITE QR with Binary ContentQRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");// READ QR with Binary ContentvarMyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue)){Console.WriteLine("\t Binary Data Read and Written Perfectly");}else{ throw new Exception("Corrupted Data");}
using IronBarCode;
using System;
using System.Linq;
// Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
// WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png");
// READ QR with Binary Content
var MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
if (BinaryData.SequenceEqual(MyReturnedData.BinaryValue))
{
Console.WriteLine("\t Binary Data Read and Written Perfectly");
}
else
{
throw new Exception("Corrupted Data");
}
ImportsMicrosoft.VisualBasicImportsIronBarCodeImportsSystemImportsSystem.Linq' Create Some Binary Data - This example equally well for Byte[] and System.IO.StreamPrivateBinaryData() AsByte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")' WRITE QR with Binary ContentQRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")' READ QR with Binary ContentDimMyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()IfBinaryData.SequenceEqual(MyReturnedData.BinaryValue) ThenConsole.WriteLine(vbTab & " Binary Data Read and Written Perfectly")ElseThrow New Exception("Corrupted Data")End If
Imports Microsoft.VisualBasic
Imports IronBarCode
Imports System
Imports System.Linq
' Create Some Binary Data - This example equally well for Byte[] and System.IO.Stream
Private BinaryData() As Byte = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")
' WRITE QR with Binary Content
QRCodeWriter.CreateQrCode(BinaryData, 500).SaveAsImage("MyBinaryQR.png")
' READ QR with Binary Content
Dim MyReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
If BinaryData.SequenceEqual(MyReturnedData.BinaryValue) Then
Console.WriteLine(vbTab & " Binary Data Read and Written Perfectly")
Else
Throw New Exception("Corrupted Data")
End If
CreateQrCodeWithLogo方法通过以下方式智能处理标志放置:
自动调整徽标大小,以保持二维码的可读性
将其放置在静默区内,以避免数据损坏
更改二维码颜色时,保持徽标的原始颜色不变
这种方法可以确保您的品牌二维码在所有扫描设备和应用程序上都能完全正常工作。
包含 Visual Studio 徽标的二维码,展示了 IronBarcode 的自动徽标尺寸调整和位置功能。
如何将二维码导出为不同格式?
IronBarcode支持多种导出格式以适应不同的使用场景。 将您的二维码导出为图片、PDF 或 HTML 文件:
using IronBarCode;using System;using System.Linq;BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });if (result != null){Console.WriteLine(result.First().Value);}
using IronBarCode;
using System;
using System.Linq;
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode });
if (result != null)
{
Console.WriteLine(result.First().Value);
}
ImportsIronBarCodeImportsSystemImportsSystem.LinqPrivate result AsBarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})If result IsNot Nothing ThenConsole.WriteLine(result.First().Value)End If
Imports IronBarCode
Imports System
Imports System.Linq
Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
If result IsNot Nothing Then
Console.WriteLine(result.First().Value)
End If
using IronBarCode;using System;using System.Linq;// Convert string to binary databyte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");// Create QR code from binary contentQRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png");// Read and verify binary data integrityvar myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();// Confirm data matches originalif (binaryData.SequenceEqual(myReturnedData.BinaryValue)){Console.WriteLine("Binary Data Read and Written Perfectly");}else{ throw new Exception("Data integrity check failed");}
using IronBarCode;
using System;
using System.Linq;
// Convert string to binary data
byte[] binaryData = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/");
// Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png");
// Read and verify binary data integrity
var myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First();
// Confirm data matches original
if (binaryData.SequenceEqual(myReturnedData.BinaryValue))
{
Console.WriteLine("Binary Data Read and Written Perfectly");
}
else
{
throw new Exception("Data integrity check failed");
}
ImportsIronBarCodeImportsSystemImportsSystem.Linq' Convert string to binary dataDim binaryData AsByte() = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")' Create QR code from binary contentQRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png")' Read and verify binary data integrityDim myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()' Confirm data matches originalIf binaryData.SequenceEqual(myReturnedData.BinaryValue) ThenConsole.WriteLine("Binary Data Read and Written Perfectly")ElseThrow New Exception("Data integrity check failed")End If
Imports IronBarCode
Imports System
Imports System.Linq
' Convert string to binary data
Dim binaryData As Byte() = System.Text.Encoding.UTF8.GetBytes("https://ironsoftware.com/csharp/barcode/")
' Create QR code from binary content
QRCodeWriter.CreateQrCode(binaryData, 500).SaveAsPng("MyBinaryQR.png")
' Read and verify binary data integrity
Dim myReturnedData = BarcodeReader.Read("MyBinaryQR.png").First()
' Confirm data matches original
If binaryData.SequenceEqual(myReturnedData.BinaryValue) Then
Console.WriteLine("Binary Data Read and Written Perfectly")
Else
Throw New Exception("Data integrity check failed")
End If
using IronBarCode;using System;using System.Linq;// Read QR code with optimized settingsBarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode});// Extract and display the decoded valueif (result != null && result.Any()){Console.WriteLine(result.First().Value);}else{Console.WriteLine("No QR codes found in the image.");}
using IronBarCode;
using System;
using System.Linq;
// Read QR code with optimized settings
BarcodeResults result = BarcodeReader.Read("QR.png", new BarcodeReaderOptions() {
ExpectBarcodeTypes = BarcodeEncoding.QRCode
});
// Extract and display the decoded value
if (result != null && result.Any())
{
Console.WriteLine(result.First().Value);
}
else
{
Console.WriteLine("No QR codes found in the image.");
}
ImportsIronBarCodeImportsSystemImportsSystem.Linq' Read QR code with optimized settingsPrivate result AsBarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})' Extract and display the decoded valueIf result IsNot NothingAndAlso result.Any() ThenConsole.WriteLine(result.First().Value)ElseConsole.WriteLine("No QR codes found in the image.")End If
Imports IronBarCode
Imports System
Imports System.Linq
' Read QR code with optimized settings
Private result As BarcodeResults = BarcodeReader.Read("QR.png", New BarcodeReaderOptions() With {.ExpectBarcodeTypes = BarcodeEncoding.QRCode})
' Extract and display the decoded value
If result IsNot Nothing AndAlso result.Any() Then
Console.WriteLine(result.First().Value)
Else
Console.WriteLine("No QR codes found in the image.")
End If
对于需要精细控制的更复杂场景:
using IronBarCode;using System;using System.Linq;// Configure advanced reading optionsBarcodeReaderOptions options = new BarcodeReaderOptions{Speed = ReadingSpeed.Faster, // Optimize for speedExpectMultipleBarcodes = false, // Single QR code expectedExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes onlyMultithreaded = true, // Enable parallel processingMaxParallelThreads = 4, // Utilize multiple CPU coresRemoveFalsePositive = true, // Filter out false detectionsImageFilters = new ImageFilterCollection() // Apply preprocessing { new AdaptiveThresholdFilter(), // Handle varying lighting new ContrastFilter(), // Enhance contrast new SharpenFilter() // Improve edge definition }};// Read with advanced configurationBarcodeResults result = BarcodeReader.Read("QR.png", options);
using IronBarCode;
using System;
using System.Linq;
// Configure advanced reading options
BarcodeReaderOptions options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Faster, // Optimize for speed
ExpectMultipleBarcodes = false, // Single QR code expected
ExpectBarcodeTypes = BarcodeEncoding.QRCode, // QR codes only
Multithreaded = true, // Enable parallel processing
MaxParallelThreads = 4, // Utilize multiple CPU cores
RemoveFalsePositive = true, // Filter out false detections
ImageFilters = new ImageFilterCollection() // Apply preprocessing
{
new AdaptiveThresholdFilter(), // Handle varying lighting
new ContrastFilter(), // Enhance contrast
new SharpenFilter() // Improve edge definition
}
};
// Read with advanced configuration
BarcodeResults result = BarcodeReader.Read("QR.png", options);
ImportsIronBarCodeImportsSystemImportsSystem.Linq' Configure advanced reading optionsDim options As New BarcodeReaderOptionsWith { .Speed = ReadingSpeed.Faster, ' Optimize for speed .ExpectMultipleBarcodes = False, ' Single QR code expected .ExpectBarcodeTypes = BarcodeEncoding.QRCode, ' QR codes only .Multithreaded = True, ' Enable parallel processing .MaxParallelThreads = 4, ' Utilize multiple CPU cores .RemoveFalsePositive = True, ' Filter out false detections .ImageFilters = New ImageFilterCollection() From { ' Apply preprocessing New AdaptiveThresholdFilter(), ' Handle varying lighting New ContrastFilter(), ' Enhance contrast New SharpenFilter() ' Improve edge definition }}' Read with advanced configurationDim result AsBarcodeResults = BarcodeReader.Read("QR.png", options)
Imports IronBarCode
Imports System
Imports System.Linq
' Configure advanced reading options
Dim options As New BarcodeReaderOptions With {
.Speed = ReadingSpeed.Faster, ' Optimize for speed
.ExpectMultipleBarcodes = False, ' Single QR code expected
.ExpectBarcodeTypes = BarcodeEncoding.QRCode, ' QR codes only
.Multithreaded = True, ' Enable parallel processing
.MaxParallelThreads = 4, ' Utilize multiple CPU cores
.RemoveFalsePositive = True, ' Filter out false detections
.ImageFilters = New ImageFilterCollection() From { ' Apply preprocessing
New AdaptiveThresholdFilter(), ' Handle varying lighting
New ContrastFilter(), ' Enhance contrast
New SharpenFilter() ' Improve edge definition
}
}
' Read with advanced configuration
Dim result As BarcodeResults = BarcodeReader.Read("QR.png", options)
Jacob Mellor 是 Iron Software 的首席技术官,也是一位开创 C# PDF 技术的有远见的工程师。作为 Iron Software 核心代码库的原始开发者,他从公司成立之初就开始塑造公司的产品架构,与首席执行官 Cameron Rimington 一起将公司转变为一家拥有 50 多名员工的公司,为 NASA、特斯拉和全球政府机构提供服务。