-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-postal-barcode-and-store-it-as-blob-in-sql-server-database-table.cs
More file actions
66 lines (61 loc) · 3.46 KB
/
Copy pathgenerate-postal-barcode-and-store-it-as-blob-in-sql-server-database-table.cs
File metadata and controls
66 lines (61 loc) · 3.46 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
58
59
60
61
62
63
64
65
66
// Title: Generate and store a Postnet barcode as a PNG BLOB
// Description: Demonstrates creating a Postnet postal barcode, converting it to PNG, and showing how to store it as a BLOB in SQL Server.
// Category-Description: This example belongs to the Aspose.BarCode barcode generation category, illustrating the use of BarcodeGenerator, BarcodeParameters, and image handling classes. Typical use cases include generating postal barcodes for mailing systems and persisting them in databases. Developers often need to create barcode images, convert them to byte arrays, and insert them into SQL Server tables as VARBINARY data.
// Prompt: Generate a postal barcode and store it as a BLOB in a SQL Server database table.
// Tags: postnet, postal barcode, barcode generation, image conversion, sql server, blob, aspose.barcode
using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.Drawing;
using Aspose.Drawing.Imaging;
/// <summary>
/// Example program that generates a Postnet postal barcode,
/// converts it to a PNG byte array, and demonstrates how it could be stored
/// as a BLOB in a SQL Server database.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// </summary>
static void Main()
{
// Initialize the barcode generator for the Postnet symbology with the data "12345".
using (var generator = new BarcodeGenerator(EncodeTypes.Postnet, "12345"))
{
// Set a short bar height specific to postal barcodes (5 points).
generator.Parameters.Barcode.Postal.PostalShortBarHeight.Point = 5f;
// Generate the barcode image as a Bitmap.
using (Bitmap bitmap = generator.GenerateBarCodeImage())
{
// Prepare a memory stream to hold the PNG representation.
using (var ms = new MemoryStream())
{
// Save the bitmap to the memory stream in PNG format.
bitmap.Save(ms, ImageFormat.Png);
byte[] imageBytes = ms.ToArray(); // Convert stream to byte array.
// -----------------------------------------------------------------
// Example of inserting the PNG byte array into a SQL Server table:
// -----------------------------------------------------------------
// using (var connection = new SqlConnection("your_connection_string"))
// {
// connection.Open();
// using (var command = new SqlCommand(
// "INSERT INTO Barcodes (Id, Image) VALUES (@Id, @Image)", connection))
// {
// command.Parameters.Add("@Id", SqlDbType.Int).Value = 1;
// command.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imageBytes;
// command.ExecuteNonQuery();
// }
// }
// -----------------------------------------------------------------
// Since the execution environment may lack SQL Server libraries,
// write the PNG file locally for demonstration purposes.
File.WriteAllBytes("postal_barcode.png", imageBytes);
Console.WriteLine("Postal barcode generated and saved to postal_barcode.png");
}
}
}
}
}