Spire.Barcode is a professional barcode library specially designed for .NET developers (C#, VB.NET, ASP.NET, .NET Core) and Java developers (J2SE and J2EE) to generate, read and scan 1D & 2D barcodes.

Thu Mar 24, 2022 9:53 pm

When using the save file option after generating a data matrix code, the saved image file is large with tons of whitespace around the data matrix code image. The dimensions of the image file is 9600x5760 pixels, which is quite large.

Am I missing some settings to eliminate white space around data matrix code itself and/or to minimize the size of generated image to just fit the rendered size of the data matrix code?

I really want to be able to generate an image that is 1 inch by 1 inch with no whitespace around it so the image can be injected into a label form, etc. as-is without having to resize the image or get rid of whitespace around the image.

Sample code:

BarcodeSettings bs = new BarcodeSettings();
bs.Type = BarCodeType.DataMatrix;
bs.ShowText = false;
bs.Unit = GraphicsUnit.Inch;
bs.BarHeight = 1;
bs.AutoResize = false;
bs.Data2D = "Just some text to include.";
BarCodeGenerator bg = new BarCodeGenerator(bs);
string filename = "TestFile.png";
bg.GenerateImage().Save(filename);
System.Diagnostics.Process.Start(filename);

mesterak
 
Posts: 7
Joined: Sun Mar 01, 2015 5:25 pm

Fri Mar 25, 2022 7:29 am

Hello,

Thank you for your inquiry.
Please try the following code to achieve your need. If you have any questions, please feel free to contact us.

Code: Select all
            BarcodeSettings bs = new BarcodeSettings();
            bs.Type = BarCodeType.DataMatrix;
            //Pixel unit
            bs.Unit = GraphicsUnit.Pixel;
            bs.ShowText = false;
            bs.Data2D = "Just some text to include.";

            bs.AutoResize = false;
            bs.X = 4;
            bs.Y = 4;
            //Convert unit Inch to Pixel
            PdfUnitConvertor unitConvertor = new PdfUnitConvertor();
            float valueInPixel = unitConvertor.ConvertUnits(1, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Pixel);
            //Set the image width and height
            bs.ImageHeight = valueInPixel;
            bs.ImageWidth = valueInPixel;

            BarCodeGenerator bg = new BarCodeGenerator(bs);
            string filename = "TestFile.png";
            bg.GenerateImage().Save(filename);
            System.Diagnostics.Process.Start(filename);


Sincerely,
kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Apr 12, 2022 9:55 am

Hi,

Greetings from E-iceblue!
Has your issue been solved? Could you please give us some feedback?

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Apr 12, 2022 6:13 pm

Where is this PdfUnitConvertor? I don't see a namespace for this.

mesterak
 
Posts: 7
Joined: Sun Mar 01, 2015 5:25 pm

Wed Apr 13, 2022 3:47 am

Hi,

Thanks for your reply.
The PdfUnitConvertor is the interface provided by Spire.PDF. Here I used to convert the unit (Inch to Pixel). You can import the dll of Spire.pdf Pack(Hot Fix) Version 8.3.9 to use this interface, or you can convert units by other method without it.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.BarCode