using IronQr;using IronSoftware.Drawing;using System;using System.Collections.Generic;IronQr.License.LicenseKey = "License-Key";// Load the image file that contains the QR Codevar inputImage = AnyBitmap.FromFile("QRCode.png");// Prepare the image for QR code detectionQrImageInput qrInput = new QrImageInput(inputImage);// Initialize the QR Code readerQrReader qrReader = new QrReader();// Execute QR Code reading on the provided imageIEnumerable<QrResult> qrResults = qrReader.Read(qrInput);// Assuming you have the QR results in qrResults as beforeforeach (var result in qrResults){Console.WriteLine(result.Value); // Print the QR code content to the console}
using IronQr;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
IronQr.License.LicenseKey = "License-Key";
// Load the image file that contains the QR Code
var inputImage = AnyBitmap.FromFile("QRCode.png");
// Prepare the image for QR code detection
QrImageInput qrInput = new QrImageInput(inputImage);
// Initialize the QR Code reader
QrReader qrReader = new QrReader();
// Execute QR Code reading on the provided image
IEnumerable<QrResult> qrResults = qrReader.Read(qrInput);
// Assuming you have the QR results in qrResults as before
foreach (var result in qrResults)
{
Console.WriteLine(result.Value); // Print the QR code content to the console
}
ImportsIronQrImportsIronSoftware.DrawingImportsSystemImportsSystem.Collections.GenericIronQr.License.LicenseKey = "License-Key"' Load the image file that contains the QR CodeDim inputImage = AnyBitmap.FromFile("QRCode.png")' Prepare the image for QR code detectionDim qrInput As New QrImageInput(inputImage)' Initialize the QR Code readerDim qrReader As New QrReader()' Execute QR Code reading on the provided imageDim qrResults AsIEnumerable(OfQrResult) = qrReader.Read(qrInput)' Assuming you have the QR results in qrResults as beforeFor Each result In qrResultsConsole.WriteLine(result.Value) ' Print the QR code content to the consoleNext result
Imports IronQr
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
IronQr.License.LicenseKey = "License-Key"
' Load the image file that contains the QR Code
Dim inputImage = AnyBitmap.FromFile("QRCode.png")
' Prepare the image for QR code detection
Dim qrInput As New QrImageInput(inputImage)
' Initialize the QR Code reader
Dim qrReader As New QrReader()
' Execute QR Code reading on the provided image
Dim qrResults As IEnumerable(Of QrResult) = qrReader.Read(qrInput)
' Assuming you have the QR results in qrResults as before
For Each result In qrResults
Console.WriteLine(result.Value) ' Print the QR code content to the console
Next result
-混合スキャンモード: 速度と精度のバランスをとります。鮮明でない QR コードや部分的に隠れている QR コードに役立ちます。
-機械学習 (ML) スキャン モード: 高度なテクノロジーを使用して、破損した QR コードや読み取りにくい QR コードを読み取ります。検出が難しいシナリオに最適です。
-基本スキャン モード: 明確でわかりやすい QR コードを読み取るための、最もシンプルで最速の方法です。
using IronQr;using IronQr.Enum;using IronSoftware.Drawing;using System.Collections.Generic;IronQr.License.LicenseKey = "License-Key";// Load the image file that contains the QR Codevar inputImage = AnyBitmap.FromFile("QRCode.png");QrImageInput mixedScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel);IEnumerable<QrResult> mixedScanResults = new QrReader().Read(mixedScanInput);QrImageInput mlScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel);IEnumerable<QrResult> mlScanResults = new QrReader().Read(mlScanInput);QrImageInput basicScanInput = new QrImageInput(inputImage, QrScanMode.OnlyBasicScan);IEnumerable<QrResult> basicScanResults = new QrReader().Read(basicScanInput);
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.Collections.Generic;
IronQr.License.LicenseKey = "License-Key";
// Load the image file that contains the QR Code
var inputImage = AnyBitmap.FromFile("QRCode.png");
QrImageInput mixedScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel);
IEnumerable<QrResult> mixedScanResults = new QrReader().Read(mixedScanInput);
QrImageInput mlScanInput = new QrImageInput(inputImage, QrScanMode.OnlyDetectionModel);
IEnumerable<QrResult> mlScanResults = new QrReader().Read(mlScanInput);
QrImageInput basicScanInput = new QrImageInput(inputImage, QrScanMode.OnlyBasicScan);
IEnumerable<QrResult> basicScanResults = new QrReader().Read(basicScanInput);
ImportsIronQrImportsIronQr.EnumImportsIronSoftware.DrawingImportsSystem.Collections.GenericIronQr.License.LicenseKey = "License-Key"' Load the image file that contains the QR CodeDim inputImage = AnyBitmap.FromFile("QRCode.png")Dim mixedScanInput As New QrImageInput(inputImage, QrScanMode.OnlyDetectionModel)Dim mixedScanResults AsIEnumerable(OfQrResult) = (New QrReader()).Read(mixedScanInput)Dim mlScanInput As New QrImageInput(inputImage, QrScanMode.OnlyDetectionModel)Dim mlScanResults AsIEnumerable(OfQrResult) = (New QrReader()).Read(mlScanInput)Dim basicScanInput As New QrImageInput(inputImage, QrScanMode.OnlyBasicScan)Dim basicScanResults AsIEnumerable(OfQrResult) = (New QrReader()).Read(basicScanInput)
Imports IronQr
Imports IronQr.Enum
Imports IronSoftware.Drawing
Imports System.Collections.Generic
IronQr.License.LicenseKey = "License-Key"
' Load the image file that contains the QR Code
Dim inputImage = AnyBitmap.FromFile("QRCode.png")
Dim mixedScanInput As New QrImageInput(inputImage, QrScanMode.OnlyDetectionModel)
Dim mixedScanResults As IEnumerable(Of QrResult) = (New QrReader()).Read(mixedScanInput)
Dim mlScanInput As New QrImageInput(inputImage, QrScanMode.OnlyDetectionModel)
Dim mlScanResults As IEnumerable(Of QrResult) = (New QrReader()).Read(mlScanInput)
Dim basicScanInput As New QrImageInput(inputImage, QrScanMode.OnlyBasicScan)
Dim basicScanResults As IEnumerable(Of QrResult) = (New QrReader()).Read(basicScanInput)
高度なQRコードの読み取り
IronQR の高度な QR コード読み取り機能は、QR コードのスキャンとデコードに対する包括的なアプローチを提供します。 この機能セットは基本的な読み取りを超え、より深いレベルのインタラクションとデータ抽出を提供します。
using IronQr;using IronSoftware.Drawing;using System;using System.Collections.Generic;IronQr.License.LicenseKey = "License-Key";var imageToScan = AnyBitmap.FromFile("QRCode.png");QrImageInput qrInput = new QrImageInput(imageToScan);QrReader qrScanner = new QrReader();IEnumerable<QrResult> scanResults = qrScanner.Read(qrInput);foreach (QrResult qrResult in scanResults){Console.WriteLine(qrResult.Value);Console.WriteLine(qrResult.Url); foreach (IronSoftware.Drawing.PointF coordinate in qrResult.Points) {Console.WriteLine($"{coordinate.X}, {coordinate.Y}"); }}
using IronQr;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
IronQr.License.LicenseKey = "License-Key";
var imageToScan = AnyBitmap.FromFile("QRCode.png");
QrImageInput qrInput = new QrImageInput(imageToScan);
QrReader qrScanner = new QrReader();
IEnumerable<QrResult> scanResults = qrScanner.Read(qrInput);
foreach (QrResult qrResult in scanResults)
{
Console.WriteLine(qrResult.Value);
Console.WriteLine(qrResult.Url);
foreach (IronSoftware.Drawing.PointF coordinate in qrResult.Points)
{
Console.WriteLine($"{coordinate.X}, {coordinate.Y}");
}
}
ImportsIronQrImportsIronSoftware.DrawingImportsSystemImportsSystem.Collections.GenericIronQr.License.LicenseKey = "License-Key"Dim imageToScan = AnyBitmap.FromFile("QRCode.png")Dim qrInput As New QrImageInput(imageToScan)Dim qrScanner As New QrReader()Dim scanResults AsIEnumerable(OfQrResult) = qrScanner.Read(qrInput)For Each qrResult AsQrResultIn scanResultsConsole.WriteLine(qrResult.Value)Console.WriteLine(qrResult.Url) For Each coordinate AsIronSoftware.Drawing.PointFIn qrResult.PointsConsole.WriteLine($"{coordinate.X}, {coordinate.Y}") Next coordinateNext qrResult
Imports IronQr
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
IronQr.License.LicenseKey = "License-Key"
Dim imageToScan = AnyBitmap.FromFile("QRCode.png")
Dim qrInput As New QrImageInput(imageToScan)
Dim qrScanner As New QrReader()
Dim scanResults As IEnumerable(Of QrResult) = qrScanner.Read(qrInput)
For Each qrResult As QrResult In scanResults
Console.WriteLine(qrResult.Value)
Console.WriteLine(qrResult.Url)
For Each coordinate As IronSoftware.Drawing.PointF In qrResult.Points
Console.WriteLine($"{coordinate.X}, {coordinate.Y}")
Next coordinate
Next qrResult
C# アプリケーションで IronQR ライブラリを使用して QR コード ジェネレーターを作成するには、次の手順に注意深く従ってください。 このガイドは、Windowsフォームアプリケーションのセットアップ、IronQRライブラリのインストール、QRコードを生成するコードの記述、および出力の理解を案内します。
public QR_Generator()
{
InitializeComponent();
SetLicenseKey();
EnsureDirectoryExists(qrCodesDirectory);
}
'INSTANT VB WARNING: The following constructor is declared outside of its associated class:'ORIGINAL LINE: public QR_Generator()Public Sub New()InitializeComponent()SetLicenseKey()EnsureDirectoryExists(qrCodesDirectory)End Sub
'INSTANT VB WARNING: The following constructor is declared outside of its associated class:
'ORIGINAL LINE: public QR_Generator()
Public Sub New()
InitializeComponent()
SetLicenseKey()
EnsureDirectoryExists(qrCodesDirectory)
End Sub
PrivateShared Sub EnsureDirectoryExists(ByVal path AsString) IfNotSystem.IO.Directory.Exists(path) ThenSystem.IO.Directory.CreateDirectory(path) End IfEnd Sub
Private Shared Sub EnsureDirectoryExists(ByVal path As String)
If Not System.IO.Directory.Exists(path) Then
System.IO.Directory.CreateDirectory(path)
End If
End Sub
private voidUpdateColor(ref Color targetColor, Control display, bool isBackground){ if (select_color.ShowDialog() == DialogResult.OK) { var hexColor = ColorToHex(select_color.Color); targetColor = new Color(hexColor); display.BackColor = select_color.Color; }}
private void UpdateColor(ref Color targetColor, Control display, bool isBackground)
{
if (select_color.ShowDialog() == DialogResult.OK)
{
var hexColor = ColorToHex(select_color.Color);
targetColor = new Color(hexColor);
display.BackColor = select_color.Color;
}
}
Private Sub UpdateColor(ByRef targetColor AsColor, ByVal display AsControl, ByVal isBackground AsBoolean) If select_color.ShowDialog() = DialogResult.OKThen Dim hexColor = ColorToHex(select_color.Color) targetColor = New Color(hexColor) display.BackColor = select_color.Color End IfEnd Sub
Private Sub UpdateColor(ByRef targetColor As Color, ByVal display As Control, ByVal isBackground As Boolean)
If select_color.ShowDialog() = DialogResult.OK Then
Dim hexColor = ColorToHex(select_color.Color)
targetColor = New Color(hexColor)
display.BackColor = select_color.Color
End If
End Sub
4.5 ロゴの追加
ユーザーがロゴを選択できるようにします。
private void btn_logo_Click(object sender, EventArgs e){ if (select_logo.ShowDialog() == DialogResult.OK) { try { logoBmp = new AnyBitmap(select_logo.FileName); selected_logo.Image = Image.FromFile(select_logo.FileName); } catch (Exception ex) {ShowError("An error occurred while loading the logo", ex.Message); } }}
private void btn_logo_Click(object sender, EventArgs e)
{
if (select_logo.ShowDialog() == DialogResult.OK)
{
try
{
logoBmp = new AnyBitmap(select_logo.FileName);
selected_logo.Image = Image.FromFile(select_logo.FileName);
}
catch (Exception ex)
{
ShowError("An error occurred while loading the logo", ex.Message);
}
}
}
Private Sub btn_logo_Click(ByVal sender AsObject, ByVal e AsEventArgs) If select_logo.ShowDialog() = DialogResult.OKThenTry logoBmp = New AnyBitmap(select_logo.FileName) selected_logo.Image = Image.FromFile(select_logo.FileName)Catch ex AsExceptionShowError("An error occurred while loading the logo", ex.Message)EndTry End IfEnd Sub
Private Sub btn_logo_Click(ByVal sender As Object, ByVal e As EventArgs)
If select_logo.ShowDialog() = DialogResult.OK Then
Try
logoBmp = New AnyBitmap(select_logo.FileName)
selected_logo.Image = Image.FromFile(select_logo.FileName)
Catch ex As Exception
ShowError("An error occurred while loading the logo", ex.Message)
End Try
End If
End Sub
Private Function CreateStyleOptions() AsQrStyleOptions'INSTANT VB TODO TASK: Throw expressions are not converted by Instant VB:'ORIGINAL LINE: return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; Return New QrStyleOptionsWith { .BackgroundColor = bgColor, .Color = color, .Dimensions = If(txt_dimension.Value > 0, Convert.ToInt32(txt_dimension.Value), throw New ArgumentException("Please select valid dimensions!")), .Margins = Convert.ToInt32(txt_margin.Value), .Logo = If(logoBmp IsNot Nothing, New QrLogoWith { .Bitmap = logoBmp, .Width = 50, .Height = 50, .CornerRadius = 5 }, Nothing) }End Function
Private Function CreateStyleOptions() As QrStyleOptions
'INSTANT VB TODO TASK: Throw expressions are not converted by Instant VB:
'ORIGINAL LINE: return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null };
Return New QrStyleOptions With {
.BackgroundColor = bgColor,
.Color = color,
.Dimensions = If(txt_dimension.Value > 0, Convert.ToInt32(txt_dimension.Value), throw New ArgumentException("Please select valid dimensions!")),
.Margins = Convert.ToInt32(txt_margin.Value),
.Logo = If(logoBmp IsNot Nothing, New QrLogo With {
.Bitmap = logoBmp,
.Width = 50,
.Height = 50,
.CornerRadius = 5
}, Nothing)
}
End Function
4.7 QRコードの保存
生成された QR コードの保存を処理します。
private void btn_save_Click(object sender, EventArgs e){SaveQRCode();}private voidSaveQRCode(){ if (pictureBox.Image == null) {MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName));MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) {ShowError("An error occurred while saving the QR code", ex.Message); } }}
private void btn_save_Click(object sender, EventArgs e)
{
SaveQRCode();
}
private void SaveQRCode()
{
if (pictureBox.Image == null)
{
MessageBox.Show("There is no QR code to save.", "Error");
return;
}
saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg";
saveFileDialog.Title = "Save QR Code";
saveFileDialog.FileName = "QRCode";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName));
MessageBox.Show("QR Code has been saved!", "Success");
}
catch (Exception ex)
{
ShowError("An error occurred while saving the QR code", ex.Message);
}
}
}
Private Sub btn_save_Click(ByVal sender AsObject, ByVal e AsEventArgs)SaveQRCode()End SubPrivate Sub SaveQRCode() If pictureBox.ImageIs Nothing ThenMessageBox.Show("There is no QR code to save.", "Error") Return End If saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg" saveFileDialog.Title = "Save QR Code" saveFileDialog.FileName = "QRCode" If saveFileDialog.ShowDialog() = DialogResult.OKThenTry pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName))MessageBox.Show("QR Code has been saved!", "Success")Catch ex AsExceptionShowError("An error occurred while saving the QR code", ex.Message)EndTry End IfEnd Sub
Private Sub btn_save_Click(ByVal sender As Object, ByVal e As EventArgs)
SaveQRCode()
End Sub
Private Sub SaveQRCode()
If pictureBox.Image Is Nothing Then
MessageBox.Show("There is no QR code to save.", "Error")
Return
End If
saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"
saveFileDialog.Title = "Save QR Code"
saveFileDialog.FileName = "QRCode"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Try
pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName))
MessageBox.Show("QR Code has been saved!", "Success")
Catch ex As Exception
ShowError("An error occurred while saving the QR code", ex.Message)
End Try
End If
End Sub
Private Function DetermineImageFormat(ByVal filePath AsString) AsSystem.Drawing.Imaging.ImageFormat Return If(System.IO.Path.GetExtension(filePath).ToLower() = ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg, System.Drawing.Imaging.ImageFormat.Png)End Function
Private Function DetermineImageFormat(ByVal filePath As String) As System.Drawing.Imaging.ImageFormat
Return If(System.IO.Path.GetExtension(filePath).ToLower() = ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg, System.Drawing.Imaging.ImageFormat.Png)
End Function
Private Sub btn_reset_Click(ByVal sender AsObject, ByVal e AsEventArgs)ResetFields()End SubPrivate Sub ResetFields() txt_QR.Text = String.Empty txt_dimension.Value = 200 txt_margin.Value = 0 bgColor = Color.White color = Color.Black txt_selected_color.BackColor = System.Drawing.Color.White txt_selected_bgcolor.BackColor = System.Drawing.Color.Black logoBmp = Nothing selected_logo.Image = Nothing pictureBox.Image = NothingEnd Sub
Private Sub btn_reset_Click(ByVal sender As Object, ByVal e As EventArgs)
ResetFields()
End Sub
Private Sub ResetFields()
txt_QR.Text = String.Empty
txt_dimension.Value = 200
txt_margin.Value = 0
bgColor = Color.White
color = Color.Black
txt_selected_color.BackColor = System.Drawing.Color.White
txt_selected_bgcolor.BackColor = System.Drawing.Color.Black
logoBmp = Nothing
selected_logo.Image = Nothing
pictureBox.Image = Nothing
End Sub
using IronQr;using IronSoftware.Drawing;using Color = IronSoftware.Drawing.Color;namespace IronQR_QR_Generator_WinForms{ public partial class QR_Generator : Form { string qrCodesDirectory = System.IO.Path.Combine(Application.StartupPath, "QR Codes"); Color bgColor = Color.White; Color color = Color.Black; AnyBitmap? logoBmp = null; publicQR_Generator() {InitializeComponent();SetLicenseKey();EnsureDirectoryExists(qrCodesDirectory); } private static voidSetLicenseKey() {IronQr.License.LicenseKey = "License-Key"; } private static voidEnsureDirectoryExists(string path) { if (!System.IO.Directory.Exists(path)) {System.IO.Directory.CreateDirectory(path); } } private void btn_color_Click(object sender, EventArgs e) {UpdateColor(ref color, txt_selected_color, false); } private void btn_background_Click(object sender, EventArgs e) {UpdateColor(ref bgColor, txt_selected_bgcolor, true); } private stringColorToHex(System.Drawing.Color color) { return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; } private voidUpdateColor(ref Color targetColor, Control display, bool isBackground) { if (select_color.ShowDialog() == DialogResult.OK) { var hexColor = ColorToHex(select_color.Color); targetColor = new Color(hexColor); display.BackColor = select_color.Color; } } private void btn_logo_Click(object sender, EventArgs e) { if (select_logo.ShowDialog() == DialogResult.OK) { try { logoBmp = new AnyBitmap(select_logo.FileName); selected_logo.Image = Image.FromFile(select_logo.FileName); } catch (Exception ex) {ShowError("An error occurred while loading the logo", ex.Message); } } } private void btn_generate_Click(object sender, EventArgs e) {GenerateQRCode(); } private voidGenerateQRCode() { try { var options = new QrOptions(QrErrorCorrectionLevel.High); var myQr = QrWriter.Write(txt_QR.Text, options); var style = CreateStyleOptions(); var qrImage = myQr.Save(style); var fileName = $"{DateTime.Now:yyyyMMddHHmmssfff}_QR.png"; var fullPath = System.IO.Path.Combine(qrCodesDirectory, fileName); qrImage.SaveAs(fullPath); pictureBox.Image = Image.FromFile(fullPath); } catch (Exception ex) {ShowError("An error occurred during QR code generation or saving", ex.Message); } } private QrStyleOptionsCreateStyleOptions() { return new QrStyleOptions {BackgroundColor = bgColor,Color = color,Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"),Margins = Convert.ToInt32(txt_margin.Value),Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; } private void btn_save_Click(object sender, EventArgs e) {SaveQRCode(); } private voidSaveQRCode() { if (pictureBox.Image == null) {MessageBox.Show("There is no QR code to save.", "Error"); return; } saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"; saveFileDialog.Title = "Save QR Code"; saveFileDialog.FileName = "QRCode"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { try { pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName));MessageBox.Show("QR Code has been saved!", "Success"); } catch (Exception ex) {ShowError("An error occurred while saving the QR code", ex.Message); } } } private System.Drawing.Imaging.ImageFormatDetermineImageFormat(string filePath) { returnSystem.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png; } private void btn_reset_Click(object sender, EventArgs e) {ResetFields(); } private voidResetFields() { txt_QR.Text = string.Empty; txt_dimension.Value = 200; txt_margin.Value = 0; bgColor = Color.White; color = Color.Black; txt_selected_color.BackColor = bgColor; txt_selected_bgcolor.BackColor = color; logoBmp = null; selected_logo.Image = null; pictureBox.Image = null; } private static voidShowError(string title, string message) {MessageBox.Show($"{title}: {message}", "Error"); } }}
using IronQr;
using IronSoftware.Drawing;
using Color = IronSoftware.Drawing.Color;
namespace IronQR_QR_Generator_WinForms
{
public partial class QR_Generator : Form
{
string qrCodesDirectory = System.IO.Path.Combine(Application.StartupPath, "QR Codes");
Color bgColor = Color.White;
Color color = Color.Black;
AnyBitmap? logoBmp = null;
public QR_Generator()
{
InitializeComponent();
SetLicenseKey();
EnsureDirectoryExists(qrCodesDirectory);
}
private static void SetLicenseKey()
{
IronQr.License.LicenseKey = "License-Key";
}
private static void EnsureDirectoryExists(string path)
{
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
}
private void btn_color_Click(object sender, EventArgs e)
{
UpdateColor(ref color, txt_selected_color, false);
}
private void btn_background_Click(object sender, EventArgs e)
{
UpdateColor(ref bgColor, txt_selected_bgcolor, true);
}
private string ColorToHex(System.Drawing.Color color)
{
return $"#{color.R:X2}{color.G:X2}{color.B:X2}";
}
private void UpdateColor(ref Color targetColor, Control display, bool isBackground)
{
if (select_color.ShowDialog() == DialogResult.OK)
{
var hexColor = ColorToHex(select_color.Color);
targetColor = new Color(hexColor);
display.BackColor = select_color.Color;
}
}
private void btn_logo_Click(object sender, EventArgs e)
{
if (select_logo.ShowDialog() == DialogResult.OK)
{
try
{
logoBmp = new AnyBitmap(select_logo.FileName);
selected_logo.Image = Image.FromFile(select_logo.FileName);
}
catch (Exception ex)
{
ShowError("An error occurred while loading the logo", ex.Message);
}
}
}
private void btn_generate_Click(object sender, EventArgs e)
{
GenerateQRCode();
}
private void GenerateQRCode()
{
try
{
var options = new QrOptions(QrErrorCorrectionLevel.High);
var myQr = QrWriter.Write(txt_QR.Text, options);
var style = CreateStyleOptions();
var qrImage = myQr.Save(style);
var fileName = $"{DateTime.Now:yyyyMMddHHmmssfff}_QR.png";
var fullPath = System.IO.Path.Combine(qrCodesDirectory, fileName);
qrImage.SaveAs(fullPath);
pictureBox.Image = Image.FromFile(fullPath);
}
catch (Exception ex)
{
ShowError("An error occurred during QR code generation or saving", ex.Message);
}
}
private QrStyleOptions CreateStyleOptions()
{
return new QrStyleOptions
{
BackgroundColor = bgColor,
Color = color,
Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"),
Margins = Convert.ToInt32(txt_margin.Value),
Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null
};
}
private void btn_save_Click(object sender, EventArgs e)
{
SaveQRCode();
}
private void SaveQRCode()
{
if (pictureBox.Image == null)
{
MessageBox.Show("There is no QR code to save.", "Error");
return;
}
saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg";
saveFileDialog.Title = "Save QR Code";
saveFileDialog.FileName = "QRCode";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName));
MessageBox.Show("QR Code has been saved!", "Success");
}
catch (Exception ex)
{
ShowError("An error occurred while saving the QR code", ex.Message);
}
}
}
private System.Drawing.Imaging.ImageFormat DetermineImageFormat(string filePath)
{
return System.IO.Path.GetExtension(filePath).ToLower() == ".jpg" ? System.Drawing.Imaging.ImageFormat.Jpeg : System.Drawing.Imaging.ImageFormat.Png;
}
private void btn_reset_Click(object sender, EventArgs e)
{
ResetFields();
}
private void ResetFields()
{
txt_QR.Text = string.Empty;
txt_dimension.Value = 200;
txt_margin.Value = 0;
bgColor = Color.White;
color = Color.Black;
txt_selected_color.BackColor = bgColor;
txt_selected_bgcolor.BackColor = color;
logoBmp = null;
selected_logo.Image = null;
pictureBox.Image = null;
}
private static void ShowError(string title, string message)
{
MessageBox.Show($"{title}: {message}", "Error");
}
}
}
ImportsIronQrImportsIronSoftware.DrawingImportsColor = IronSoftware.Drawing.ColorNamespaceIronQR_QR_Generator_WinFormsPartialPublic Class QR_GeneratorInheritsForm Private qrCodesDirectory AsString = System.IO.Path.Combine(Application.StartupPath, "QR Codes") Private bgColor AsColor = Color.White Private color AsColor = Color.Black Private logoBmp? AsAnyBitmap = Nothing Public Sub New()InitializeComponent()SetLicenseKey()EnsureDirectoryExists(qrCodesDirectory) End Sub PrivateShared Sub SetLicenseKey()IronQr.License.LicenseKey = "License-Key" End Sub PrivateShared Sub EnsureDirectoryExists(ByVal path AsString) IfNotSystem.IO.Directory.Exists(path) ThenSystem.IO.Directory.CreateDirectory(path) End If End Sub Private Sub btn_color_Click(ByVal sender AsObject, ByVal e AsEventArgs)UpdateColor(color, txt_selected_color, False) End Sub Private Sub btn_background_Click(ByVal sender AsObject, ByVal e AsEventArgs)UpdateColor(bgColor, txt_selected_bgcolor, True) End Sub Private Function ColorToHex(ByVal color AsSystem.Drawing.Color) AsString Return $"#{color.R:X2}{color.G:X2}{color.B:X2}" End Function Private Sub UpdateColor(ByRef targetColor AsColor, ByVal display AsControl, ByVal isBackground AsBoolean) If select_color.ShowDialog() = System.Windows.Forms.DialogResult.OKThen Dim hexColor = ColorToHex(select_color.Color) targetColor = New Color(hexColor) display.BackColor = select_color.Color End If End Sub Private Sub btn_logo_Click(ByVal sender AsObject, ByVal e AsEventArgs) If select_logo.ShowDialog() = System.Windows.Forms.DialogResult.OKThenTry logoBmp = New AnyBitmap(select_logo.FileName) selected_logo.Image = Image.FromFile(select_logo.FileName)Catch ex AsExceptionShowError("An error occurred while loading the logo", ex.Message)EndTry End If End Sub Private Sub btn_generate_Click(ByVal sender AsObject, ByVal e AsEventArgs)GenerateQRCode() End Sub Private Sub GenerateQRCode()Try Dim options = New QrOptions(QrErrorCorrectionLevel.High) Dim myQr = QrWriter.Write(txt_QR.Text, options) Dim style = CreateStyleOptions() Dim qrImage = myQr.Save(style) Dim fileName = $"{DateTime.Now:yyyyMMddHHmmssfff}_QR.png" Dim fullPath = System.IO.Path.Combine(qrCodesDirectory, fileName) qrImage.SaveAs(fullPath) pictureBox.Image = Image.FromFile(fullPath)Catch ex AsExceptionShowError("An error occurred during QR code generation or saving", ex.Message)EndTry End Sub Private Function CreateStyleOptions() AsQrStyleOptions'INSTANT VB TODO TASK: Throw expressions are not converted by Instant VB:'ORIGINAL LINE: return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null }; Return New QrStyleOptionsWith { .BackgroundColor = bgColor, .Color = color, .Dimensions = If(txt_dimension.Value > 0, Convert.ToInt32(txt_dimension.Value), throw New ArgumentException("Please select valid dimensions!")), .Margins = Convert.ToInt32(txt_margin.Value), .Logo = If(logoBmp IsNot Nothing, New QrLogoWith { .Bitmap = logoBmp, .Width = 50, .Height = 50, .CornerRadius = 5 }, Nothing) } End Function Private Sub btn_save_Click(ByVal sender AsObject, ByVal e AsEventArgs)SaveQRCode() End Sub Private Sub SaveQRCode() If pictureBox.ImageIs Nothing ThenMessageBox.Show("There is no QR code to save.", "Error") Return End If saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg" saveFileDialog.Title = "Save QR Code" saveFileDialog.FileName = "QRCode" If saveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OKThenTry pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName))MessageBox.Show("QR Code has been saved!", "Success")Catch ex AsExceptionShowError("An error occurred while saving the QR code", ex.Message)EndTry End If End Sub Private Function DetermineImageFormat(ByVal filePath AsString) AsSystem.Drawing.Imaging.ImageFormat Return If(System.IO.Path.GetExtension(filePath).ToLower() = ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg, System.Drawing.Imaging.ImageFormat.Png) End Function Private Sub btn_reset_Click(ByVal sender AsObject, ByVal e AsEventArgs)ResetFields() End Sub Private Sub ResetFields() txt_QR.Text = String.Empty txt_dimension.Value = 200 txt_margin.Value = 0 bgColor = Color.White color = Color.Black txt_selected_color.BackColor = bgColor txt_selected_bgcolor.BackColor = color logoBmp = Nothing selected_logo.Image = Nothing pictureBox.Image = Nothing End Sub PrivateShared Sub ShowError(ByVal title AsString, ByVal message AsString)MessageBox.Show($"{title}: {message}", "Error") End Sub End ClassEndNamespace
Imports IronQr
Imports IronSoftware.Drawing
Imports Color = IronSoftware.Drawing.Color
Namespace IronQR_QR_Generator_WinForms
Partial Public Class QR_Generator
Inherits Form
Private qrCodesDirectory As String = System.IO.Path.Combine(Application.StartupPath, "QR Codes")
Private bgColor As Color = Color.White
Private color As Color = Color.Black
Private logoBmp? As AnyBitmap = Nothing
Public Sub New()
InitializeComponent()
SetLicenseKey()
EnsureDirectoryExists(qrCodesDirectory)
End Sub
Private Shared Sub SetLicenseKey()
IronQr.License.LicenseKey = "License-Key"
End Sub
Private Shared Sub EnsureDirectoryExists(ByVal path As String)
If Not System.IO.Directory.Exists(path) Then
System.IO.Directory.CreateDirectory(path)
End If
End Sub
Private Sub btn_color_Click(ByVal sender As Object, ByVal e As EventArgs)
UpdateColor(color, txt_selected_color, False)
End Sub
Private Sub btn_background_Click(ByVal sender As Object, ByVal e As EventArgs)
UpdateColor(bgColor, txt_selected_bgcolor, True)
End Sub
Private Function ColorToHex(ByVal color As System.Drawing.Color) As String
Return $"#{color.R:X2}{color.G:X2}{color.B:X2}"
End Function
Private Sub UpdateColor(ByRef targetColor As Color, ByVal display As Control, ByVal isBackground As Boolean)
If select_color.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim hexColor = ColorToHex(select_color.Color)
targetColor = New Color(hexColor)
display.BackColor = select_color.Color
End If
End Sub
Private Sub btn_logo_Click(ByVal sender As Object, ByVal e As EventArgs)
If select_logo.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
logoBmp = New AnyBitmap(select_logo.FileName)
selected_logo.Image = Image.FromFile(select_logo.FileName)
Catch ex As Exception
ShowError("An error occurred while loading the logo", ex.Message)
End Try
End If
End Sub
Private Sub btn_generate_Click(ByVal sender As Object, ByVal e As EventArgs)
GenerateQRCode()
End Sub
Private Sub GenerateQRCode()
Try
Dim options = New QrOptions(QrErrorCorrectionLevel.High)
Dim myQr = QrWriter.Write(txt_QR.Text, options)
Dim style = CreateStyleOptions()
Dim qrImage = myQr.Save(style)
Dim fileName = $"{DateTime.Now:yyyyMMddHHmmssfff}_QR.png"
Dim fullPath = System.IO.Path.Combine(qrCodesDirectory, fileName)
qrImage.SaveAs(fullPath)
pictureBox.Image = Image.FromFile(fullPath)
Catch ex As Exception
ShowError("An error occurred during QR code generation or saving", ex.Message)
End Try
End Sub
Private Function CreateStyleOptions() As QrStyleOptions
'INSTANT VB TODO TASK: Throw expressions are not converted by Instant VB:
'ORIGINAL LINE: return new QrStyleOptions { BackgroundColor = bgColor, Color = color, Dimensions = txt_dimension.Value > 0 ? Convert.ToInt32(txt_dimension.Value) : throw new ArgumentException("Please select valid dimensions!"), Margins = Convert.ToInt32(txt_margin.Value), Logo = logoBmp != null ? new QrLogo { Bitmap = logoBmp, Width = 50, Height = 50, CornerRadius = 5 } : null };
Return New QrStyleOptions With {
.BackgroundColor = bgColor,
.Color = color,
.Dimensions = If(txt_dimension.Value > 0, Convert.ToInt32(txt_dimension.Value), throw New ArgumentException("Please select valid dimensions!")),
.Margins = Convert.ToInt32(txt_margin.Value),
.Logo = If(logoBmp IsNot Nothing, New QrLogo With {
.Bitmap = logoBmp,
.Width = 50,
.Height = 50,
.CornerRadius = 5
}, Nothing)
}
End Function
Private Sub btn_save_Click(ByVal sender As Object, ByVal e As EventArgs)
SaveQRCode()
End Sub
Private Sub SaveQRCode()
If pictureBox.Image Is Nothing Then
MessageBox.Show("There is no QR code to save.", "Error")
Return
End If
saveFileDialog.Filter = "PNG Files|*.png|JPEG Files|*.jpg"
saveFileDialog.Title = "Save QR Code"
saveFileDialog.FileName = "QRCode"
If saveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
pictureBox.Image.Save(saveFileDialog.FileName, DetermineImageFormat(saveFileDialog.FileName))
MessageBox.Show("QR Code has been saved!", "Success")
Catch ex As Exception
ShowError("An error occurred while saving the QR code", ex.Message)
End Try
End If
End Sub
Private Function DetermineImageFormat(ByVal filePath As String) As System.Drawing.Imaging.ImageFormat
Return If(System.IO.Path.GetExtension(filePath).ToLower() = ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg, System.Drawing.Imaging.ImageFormat.Png)
End Function
Private Sub btn_reset_Click(ByVal sender As Object, ByVal e As EventArgs)
ResetFields()
End Sub
Private Sub ResetFields()
txt_QR.Text = String.Empty
txt_dimension.Value = 200
txt_margin.Value = 0
bgColor = Color.White
color = Color.Black
txt_selected_color.BackColor = bgColor
txt_selected_bgcolor.BackColor = color
logoBmp = Nothing
selected_logo.Image = Nothing
pictureBox.Image = Nothing
End Sub
Private Shared Sub ShowError(ByVal title As String, ByVal message As String)
MessageBox.Show($"{title}: {message}", "Error")
End Sub
End Class
End Namespace
レイアウトは入力、スタイリング、出力、およびアクションのセクションにきちんと整理されています。
アプリケーションを実行すると、入力、スタイル、出力、アクションのセクションで整理されたメイン ウィンドウが表示されます。 ユーザー インターフェイスに従ってデータを入力し、QR コードをカスタマイズし、必要に応じて QR コードを生成して保存します。
What is the suggested way to handle errors when using IronQR?
It's recommended to implement error handling in your application to catch exceptions that may occur during QR code generation or reading, ensuring a smooth experience.
Can I run the QR code generator application on different platforms?
Yes, with IronQR's compatibility across various .NET frameworks, you can run the QR code generator application on multiple platforms like Windows, Linux, and macOS.