-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace-current-bitmap-source-using-setbarcodeimage-to-process-different-in-memory-image.cs
More file actions
69 lines (62 loc) · 2.87 KB
/
Copy pathreplace-current-bitmap-source-using-setbarcodeimage-to-process-different-in-memory-image.cs
File metadata and controls
69 lines (62 loc) · 2.87 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
67
68
69
using System;
using System.IO;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Drawing;
using Aspose.Drawing.Imaging;
/// <summary>
/// Demonstrates generating a barcode, reading it, and then re‑reading after
/// replacing the image source in the same <see cref="BarCodeReader"/> instance.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// </summary>
static void Main()
{
// Generate a Code128 barcode image in memory.
using (var generator = new BarcodeGenerator(EncodeTypes.Code128, "123456"))
{
// Store the generated image in a memory stream.
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
ms.Position = 0; // Reset stream position for reading.
// Load the generated image into a Bitmap object.
using (var originalBitmap = new Bitmap(ms))
{
// Create a BarCodeReader that can decode all supported barcode types.
using (var reader = new BarCodeReader(originalBitmap, DecodeType.AllSupportedTypes))
{
Console.WriteLine("Reading barcode from original image:");
// Iterate through all detected barcodes and output their type and text.
foreach (var result in reader.ReadBarCodes())
{
Console.WriteLine($" Type: {result.CodeTypeName}, Text: {result.CodeText}");
}
// Create a new in‑memory bitmap with a white background,
// matching the size of the original bitmap.
using (var newBitmap = new Bitmap(originalBitmap.Width, originalBitmap.Height))
{
using (var graphics = Graphics.FromImage(newBitmap))
{
graphics.Clear(Color.White); // Fill the bitmap with white.
// Optionally draw additional graphics here.
}
// Replace the image source inside the existing reader with the new bitmap.
reader.SetBarCodeImage(newBitmap);
Console.WriteLine("Reading barcode after SetBarCodeImage:");
// Read barcodes again from the new image source.
foreach (var result in reader.ReadBarCodes())
{
Console.WriteLine($" Type: {result.CodeTypeName}, Text: {result.CodeText}");
}
}
}
}
}
}
}
}