When you use Aspose.Pdf.Devices.PngDevice to extract a datamatrix from a PDF as a PNG image,
it will not render correctly.
I’m using the following code to extract the image:
private static string GetPngFileDatamatrixFromPdf()
{
Aspose.Pdf.Document document = new Aspose.Pdf.Document(@"C:\Temp\AsposePdfFileDatamatrixAsVectorGraphicNotWorkCorrektByPngDevice.pdf");
// Convert the page to an image (fit to the whole page)
Resolution resolution = new Resolution(300); // DPI setting
Aspose.Pdf.Devices.PngDevice pngDevice = new PngDevice(resolution);
System.Drawing.Rectangle rectangle = new (44, 1676, 111, 103 );
using (MemoryStream ms = new MemoryStream())
{
pngDevice.Process(document.Pages.First(), ms);
// Load the full-page image into a Bitmap
using (Bitmap fullBitmap = new Bitmap(ms))
{
try
{
// Crop the selected region
using (Bitmap croppedBitmap = fullBitmap.Clone(new System.Drawing.Rectangle(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height), fullBitmap.PixelFormat))
{
//Optional write the datamatrix picture to output path
string bitmapPath = System.IO.Path.Combine(@"C:\Temp\WrongDataMatrix.png");
croppedBitmap.Save(bitmapPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading DataMatrix: " + ex.Message);
}
}
}
return "";
}
AsposePdfFileDatamatrixAsVectorGraphicNotWorkCorrektByPngDevice.pdf (7,2 KB)
WrongDataMatrix.png (240 Bytes)
CorrectDataMatrix.PNG (1,0 KB)