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
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
QR碼中的二進位編碼提供多項優勢:
效率:以緊湊的二進位格式儲存資料
多功能性:處理任何資料型別(文件、加密內容、序列化物件)
完整性:保留準確的字節序列而不發生編碼問題
此特徵使IronBarcode與基本QR碼程式庫區分開來,在您的應用中實現複雜的資料交換情境。
儲存二進位資料的QR碼,展示IronBarcode的進階編碼能力
How Do I Read QR Codes in C#?
IronBarcode提供靈活的QR碼讀取功能。 這是最簡單的方法:
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)