-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-gs1-datamatrix-barcode-retrieve-its-byte-array-and-send-it-via-http-response.cs
More file actions
57 lines (50 loc) · 2.98 KB
/
Copy pathgenerate-gs1-datamatrix-barcode-retrieve-its-byte-array-and-send-it-via-http-response.cs
File metadata and controls
57 lines (50 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Title: Generate GS1 DataMatrix Barcode and Retrieve Byte Array
// Description: This example creates a GS1 DataMatrix barcode, extracts its PNG byte array, and demonstrates how it could be sent in an HTTP response.
// Category-Description: Aspose.BarCode barcode generation examples that illustrate creating various symbologies, configuring image parameters, and obtaining raw image data. Developers often need to generate barcodes on the fly for web APIs, embed them in documents, or transmit them over HTTP. This snippet showcases the BarcodeGenerator class, image format handling, and byte array extraction, which are common tasks in e‑commerce, logistics, and inventory systems.
// Prompt: Generate a GS1 DataMatrix barcode, retrieve its byte array, and send it via HTTP response.
// Tags: gs1, datamatrix, barcode, generation, bytearray, http response, aspose.barcode, aspnet
using System;
using System.IO;
using System.Text;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.Drawing.Imaging;
/// <summary>
/// Demonstrates how to generate a GS1 DataMatrix barcode, obtain its PNG byte array,
/// and illustrate the data that would be sent in an HTTP response.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the console application.
/// Generates the barcode, extracts the byte array, and prints a Base64 representation.
/// </summary>
static void Main()
{
// NOTE: The snippet runner is a console application and cannot host an HTTP server.
// Therefore, the barcode byte array is generated and printed as a Base64 string,
// which represents the data that would be sent in an HTTP response.
// Define a sample GS1 Application Identifier (AI) payload for the barcode.
const string gs1CodeText = "(01)12345678901231";
// Initialize the barcode generator for the GS1 DataMatrix symbology.
using (var generator = new BarcodeGenerator(EncodeTypes.GS1DataMatrix, gs1CodeText))
{
// Optional: adjust image dimensions and resolution to meet specific requirements.
generator.Parameters.ImageWidth.Point = 300f;
generator.Parameters.ImageHeight.Point = 300f;
generator.Parameters.Resolution = 96;
// Generate the barcode image and write it into a memory stream.
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
byte[] barcodeBytes = ms.ToArray(); // Retrieve the raw PNG byte array.
// Output the length of the generated byte array for verification.
Console.WriteLine($"Generated barcode byte array length: {barcodeBytes.Length}");
// Convert the byte array to a Base64 string to simulate an HTTP response body.
string base64 = Convert.ToBase64String(barcodeBytes);
Console.WriteLine("Base64-encoded PNG barcode:");
Console.WriteLine(base64);
}
}
}
}