Skip to content

Commit 91381db

Browse files
Merge pull request #41 from aspose-barcode/agent/swiss-qr-code/2026-04-26-215753
Add generated Aspose.BarCode examples for swiss-qr-code
2 parents 8e25599 + 70c2c8b commit 91381db

2 files changed

Lines changed: 68 additions & 59 deletions

swiss-qr-code/compare-swiss-qr-code-image-dimensions-and-file-size-using-different-margin-and-module-size-configurations.cs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,61 @@
11
using System;
22
using System.IO;
3+
using Aspose.BarCode;
34
using Aspose.BarCode.ComplexBarcode;
45
using Aspose.BarCode.Generation;
56
using Aspose.Drawing;
6-
using Aspose.Drawing.Imaging;
77

88
class Program
99
{
1010
static void Main()
1111
{
12-
// Sample Swiss QR bill data (valid IBAN, amount, version)
13-
var swissCodetext = new SwissQRCodetext();
14-
swissCodetext.Bill.Account = "CH9300762011623852957";
15-
swissCodetext.Bill.Amount = 199.95m;
16-
swissCodetext.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
17-
swissCodetext.Bill.Creditor.CountryCode = "CH";
18-
swissCodetext.Bill.Creditor.Name = "John Doe";
19-
20-
// Configurations to test: module size (XDimension) and margin (padding)
21-
float[] moduleSizes = { 2f, 4f };
22-
float[] margins = { 0f, 5f, 10f };
23-
24-
foreach (float moduleSize in moduleSizes)
12+
// Define different configurations: margin (padding) in points and module size (XDimension) in points
13+
var configurations = new (float Margin, float ModuleSize)[]
2514
{
26-
foreach (float margin in margins)
15+
(5f, 2f),
16+
(10f, 2f),
17+
(5f, 3f),
18+
(10f, 3f)
19+
};
20+
21+
foreach (var cfg in configurations)
22+
{
23+
// Prepare Swiss QR bill data (mandatory fields)
24+
var swissQr = new SwissQRCodetext();
25+
swissQr.Bill.Creditor.Name = "John Doe";
26+
swissQr.Bill.Creditor.CountryCode = "CH";
27+
swissQr.Bill.Account = "CH9300762011623852957";
28+
swissQr.Bill.Amount = 199.95m;
29+
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
30+
31+
// Create generator for the complex barcode
32+
using (var generator = new ComplexBarcodeGenerator(swissQr))
2733
{
28-
using (ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(swissCodetext))
29-
{
30-
// Set module size (XDimension) in points
31-
generator.Parameters.Barcode.XDimension.Point = moduleSize;
34+
// Set padding (margin) on all sides
35+
generator.Parameters.Barcode.Padding.Left.Point = cfg.Margin;
36+
generator.Parameters.Barcode.Padding.Top.Point = cfg.Margin;
37+
generator.Parameters.Barcode.Padding.Right.Point = cfg.Margin;
38+
generator.Parameters.Barcode.Padding.Bottom.Point = cfg.Margin;
3239

33-
// Set uniform padding on all sides
34-
generator.Parameters.Barcode.Padding.Left.Point = margin;
35-
generator.Parameters.Barcode.Padding.Top.Point = margin;
36-
generator.Parameters.Barcode.Padding.Right.Point = margin;
37-
generator.Parameters.Barcode.Padding.Bottom.Point = margin;
40+
// Set module size (XDimension)
41+
generator.Parameters.Barcode.XDimension.Point = cfg.ModuleSize;
3842

39-
// Save barcode to a memory stream in PNG format
40-
using (MemoryStream ms = new MemoryStream())
43+
// Use interpolation auto‑size mode so the image size follows the settings
44+
generator.Parameters.AutoSizeMode = AutoSizeMode.Interpolation;
45+
46+
// Generate bitmap to obtain dimensions
47+
using (Bitmap bitmap = generator.GenerateBarCodeImage())
48+
{
49+
int width = bitmap.Width;
50+
int height = bitmap.Height;
51+
52+
// Save to memory stream to obtain file size
53+
using (var ms = new MemoryStream())
4154
{
4255
generator.Save(ms, BarCodeImageFormat.Png);
4356
long fileSize = ms.Length;
4457

45-
// Load the image to obtain dimensions
46-
ms.Position = 0;
47-
using (Bitmap bitmap = new Bitmap(ms))
48-
{
49-
int width = bitmap.Width;
50-
int height = bitmap.Height;
51-
52-
Console.WriteLine($"ModuleSize: {moduleSize}pt, Margin: {margin}pt => Width: {width}px, Height: {height}px, FileSize: {fileSize} bytes");
53-
}
58+
Console.WriteLine($"Margin: {cfg.Margin}pt, ModuleSize: {cfg.ModuleSize}pt => Width: {width}px, Height: {height}px, FileSize: {fileSize} bytes");
5459
}
5560
}
5661
}
Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
using System;
22
using Aspose.BarCode.ComplexBarcode;
33

4-
class Program
4+
namespace SwissQRDecodeExample
55
{
6-
static void Main()
6+
class Program
77
{
8-
// Create a SwissQR codetext and populate required bill fields
9-
var swissQr = new SwissQRCodetext();
10-
swissQr.Bill.Account = "CH9300762011623852957"; // valid IBAN
11-
swissQr.Bill.Amount = 199.95m; // decimal amount with 'm' suffix
12-
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0; // required version
13-
swissQr.Bill.Creditor.CountryCode = "CH"; // country code
14-
swissQr.Bill.Creditor.Name = "John Doe"; // mandatory creditor name
8+
static void Main()
9+
{
10+
// Create a SwissQR codetext with mandatory fields
11+
var original = new SwissQRCodetext();
12+
original.Bill.Creditor.Name = "John Doe";
13+
original.Bill.Creditor.CountryCode = "CH";
14+
original.Bill.Account = "CH9300762011623852957";
15+
original.Bill.Amount = 199.95m;
16+
original.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
1517

16-
// Construct the raw codetext string that would be embedded in the barcode
17-
string rawCodetext = swissQr.GetConstructedCodetext();
18+
// Get the raw codetext string
19+
string rawCodetext = original.GetConstructedCodetext();
1820

19-
// Decode the raw codetext back into a SwissQRCodetext object
20-
SwissQRCodetext decoded = ComplexCodetextReader.TryDecodeSwissQR(rawCodetext);
21+
// Decode the raw codetext back into a SwissQRCodetext object
22+
SwissQRCodetext decoded = ComplexCodetextReader.TryDecodeSwissQR(rawCodetext);
2123

22-
if (decoded == null)
23-
{
24-
Console.WriteLine("Decoding failed: result is null.");
25-
return;
24+
if (decoded != null)
25+
{
26+
Console.WriteLine("Decoded Swiss QR Bill:");
27+
Console.WriteLine($"Creditor Name: {decoded.Bill.Creditor.Name}");
28+
Console.WriteLine($"Creditor Country: {decoded.Bill.Creditor.CountryCode}");
29+
Console.WriteLine($"Account: {decoded.Bill.Account}");
30+
Console.WriteLine($"Amount: {decoded.Bill.Amount}");
31+
Console.WriteLine($"Version: {decoded.Bill.Version}");
32+
}
33+
else
34+
{
35+
Console.WriteLine("Failed to decode Swiss QR codetext.");
36+
}
2637
}
27-
28-
// Extract and display some fields from the decoded object
29-
Console.WriteLine($"Account: {decoded.Bill.Account}");
30-
Console.WriteLine($"Amount: {decoded.Bill.Amount}");
31-
Console.WriteLine($"Version: {decoded.Bill.Version}");
32-
Console.WriteLine($"Creditor Country Code: {decoded.Bill.Creditor.CountryCode}");
33-
Console.WriteLine($"Creditor Name: {decoded.Bill.Creditor.Name}");
3438
}
3539
}

0 commit comments

Comments
 (0)