|
1 | 1 | using System; |
2 | 2 | using System.IO; |
| 3 | +using Aspose.BarCode; |
3 | 4 | using Aspose.BarCode.ComplexBarcode; |
4 | 5 | using Aspose.BarCode.Generation; |
5 | 6 | using Aspose.Drawing; |
6 | | -using Aspose.Drawing.Imaging; |
7 | 7 |
|
8 | 8 | class Program |
9 | 9 | { |
10 | 10 | static void Main() |
11 | 11 | { |
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)[] |
25 | 14 | { |
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)) |
27 | 33 | { |
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; |
32 | 39 |
|
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; |
38 | 42 |
|
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()) |
41 | 54 | { |
42 | 55 | generator.Save(ms, BarCodeImageFormat.Png); |
43 | 56 | long fileSize = ms.Length; |
44 | 57 |
|
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"); |
54 | 59 | } |
55 | 60 | } |
56 | 61 | } |
|
0 commit comments