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.

Tue Oct 13, 2020 9:46 am

Hello,

i try to create a barcode and save it as picture.
The Issue is now that i cant give the barcode a fix image-height and -width.
I can size the image himself, but i cant find a possibility to maximize the barcode in the picture.
The barcode shouold fill the full height and width of the picture.
Here is my example code:
Code: Select all
            var barSet = new Spire.Barcode.BarcodeSettings()
            {
                Type = Spire.Barcode.BarCodeType.Code39,
                Data = "harglgargl",
                ShowText = false,
                AutoResize = false,
                ImageWidth = 200,
                ImageHeight = 200,
                Unit = SkiaSharp.System.Drawing.GraphicsUnit.Pixel
            };
            var barcode = new Spire.Barcode.BarCodeGenerator(barSet);
            var skImage = barcode.GenerateImage();

            // Translate SKImage to bitmap
            var bitmap = new System.Drawing.Bitmap(skImage.Width, skImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

            // copy
            using (var pixmap = new SkiaSharp.SKPixmap(new SkiaSharp.SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride))
            {
                skImage.ReadPixels(pixmap, 0, 0);
            }

            bitmap.UnlockBits(data);
            bitmap.Save(@"C:\Temp\test.bmp");

I have to modify the SKImage to bitmap to save to filesystem because the GenerateImage-method returns a SKImage-Type.

best wishes
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Tue Oct 13, 2020 11:11 am

Hello,

Thanks for your inquiry.
You can set the width and height of the barcode by setting the BarcodeSettings.BarHeight and BarcodeSettings.X properties, like the following code. If you have further questions, please feel free to let us know.
Code: Select all
            barSet.BarHeight = 50f;
            barSet.X = 5.0f;


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Oct 13, 2020 11:25 am

Hello,

thanks for your answer.
If i try this i get a full black filled image.
Code: Select all
            var barSet = new Spire.Barcode.BarcodeSettings()
            {
                Type = Spire.Barcode.BarCodeType.Code39,
                Data = "harglgargl",
                ShowText = false,
                AutoResize = false,
                ImageWidth = 200,
                ImageHeight = 200,
                Unit = SkiaSharp.System.Drawing.GraphicsUnit.Pixel,
                BarHeight = 200,
                X = 200
            };
            var barcode = new Spire.Barcode.BarCodeGenerator(barSet);
            var skImage = barcode.GenerateImage();

            // Translate SKImage to bitmap
            var bitmap = new System.Drawing.Bitmap(skImage.Width, skImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

            // copy
            using (var pixmap = new SkiaSharp.SKPixmap(new SkiaSharp.SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride))
            {
                skImage.ReadPixels(pixmap, 0, 0);
            }

            bitmap.UnlockBits(data);
            bitmap.Save(@"C:\Temp\test.bmp");

Attached my result-image.
I use Spire.Office (Latest Version 5.10.0) because i have to use also other Spire-libraries in same project.
Maybe there is not te latest Version of Spire.Barcode included?

best wishes
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Wed Oct 14, 2020 2:14 am

Hello,

Thanks for your feedback.
Kindly note that the BarcodeSettings.X property is used to set the X dimension, it is the measure of the narrowest bar in a barcode. There is not direct property to set the barcode width, you need to set a appropriate value for X property to control the barcode width. Please set it to a smaller value like this.
Code: Select all
                BarHeight = 200,
                X = 6


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Oct 14, 2020 6:48 am

Hello,

ok.
Is there any possibility to calculate the X-Value?
Maybe from number of caracters and width of barcode?

best wishes
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Wed Oct 14, 2020 8:26 am

Hi Joachim,

Thanks for your response.
Sorry there is currently no good way to calculate the X-Value to set a fixed barcode width. If you have other questions, please feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Oct 14, 2020 8:31 am

Hi Rachel,

thanks for your answer.
Maybe it is a good issue for a feature-request to set output-image-widt :) .

best wishes
Joachim

joachim_eckerl
 
Posts: 10
Joined: Thu Dec 05, 2019 12:52 pm

Wed Oct 14, 2020 11:23 am

Hello Joachim,

I got information from our Dev team that the barcode width will be affected by multiple factors, such as X dimension, the wide/narrow ratio and the number of characters, etc. The calculation of the barcode width is very complex and it is difficult to set a fixed width directly for the barcode. Hope you can understand.
If we make any progress in this regard in the future, we will let you know.

Sincerley,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.BarCode