How to create linear (or 1D) barcode with dimension of 0.8 Inch width and 3 inch height for text SEMX8957
To create a linear (1D) barcode with specific dimensions of 0.8 inches in width and 3 inches in height for the text “SEMX8957”, you can use the Aspose.BarCode library. Here’s how you can achieve this:
- Set the Barcode Type: Choose the appropriate barcode type (e.g., Code 128, Code 39, etc.).
- Adjust the Dimensions: You will need to set the
XDimension
andBarHeight
properties to match your required dimensions. TheXDimension
controls the width of the narrow bars, whileBarHeight
sets the height of the barcode.
Here is a code example demonstrating how to generate a Code 128 barcode with the specified dimensions:
// Create a BarcodeGenerator instance for Code 128
var generator = new BarCodeInstance.BarcodeGenerator("Code128", "SEMX8957");
// Set the XDimension to control the width of the bars
generator.Parameters.Barcode.XDimension = "0.1in"; // Adjust this value as needed for 0.8 inch width
// Set the BarHeight to 3 inches
generator.Parameters.Barcode.BarHeight = "3in";
// Generate the barcode image
var barcodeImage = generator.GenerateBarCodeImage();
// Display or save the barcode image as needed
document.getElementById("barcodeImage").src = barcodeImage;
generator.delete();
Important Notes:
- The
XDimension
value may need to be adjusted based on the specific barcode type and the overall width you want to achieve. The total width of the barcode will be influenced by the number of characters and the chosen barcode type. - Ensure that the
AutoSizeMode
property is set toAutoSizeMode.None
if you want to manually control the dimensions.
This approach allows you to create a barcode that meets your specified size requirements while encoding the desired text. If you have any further questions or need additional assistance, feel free to ask!
@GSA,
See the following sample code for your reference.
e.g.,
Sample code:
// Define the barcode text
string barcodeText = "SEMX8957";
// Create BarcodeGenerator object with a 1D barcode type (e.g., Code128)
Aspose.BarCode.Generation.BarcodeGenerator generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.Code128, barcodeText);
// Set the barcode size in inches (Aspose uses millimeters, so convert inches to mm)
float widthInInches = 0.8f;
float heightInInches = 3.0f;
float widthInMM = widthInInches * 25.4f;
float heightInMM = heightInInches * 25.4f;
// Set the barcode size
generator.Parameters.Barcode.XDimension.Pixels = 2;
generator.Parameters.Barcode.BarHeight.Millimeters = heightInMM;
// Set image width (this ensures the barcode width matches the required size)
generator.Parameters.ImageWidth.Millimeters = widthInMM;
generator.Parameters.ImageHeight.Millimeters = heightInMM;
// Save the barcode image
generator.Save("e:\\test2\\barcode.png", Aspose.BarCode.Generation.BarCodeImageFormat.Png);
Hope, this helps a bit.
Hi Amjad,
Thank you for the sample code. I made minor modifications to arrive at optimal size as explicit sizing does not get enforced to make sure barcode remains scannable. Appreciate your help
string barcodeText = "SEMX8957";
// Create BarcodeGenerator object with a 1D barcode type (e.g., Code128)
Aspose.BarCode.Generation.BarcodeGenerator generator = new Aspose.BarCode.Generation.BarcodeGenerator(Aspose.BarCode.Generation.EncodeTypes.Code128, barcodeText);
// Set the barcode size in inches (Aspose uses millimeters, so convert inches to mm)
float widthInInches = 1.6f;
float heightInInches = 3.0f;
float widthInMM = widthInInches * 25.4f;
float heightInMM = heightInInches * 25.4f;
// Set the barcode size
generator.Parameters.Barcode.XDimension.Pixels = 2;
generator.Parameters.Barcode.BarHeight.Millimeters = heightInMM;
// Set image width (this ensures the barcode width matches the required size)
generator.Parameters.ImageWidth.Millimeters = widthInMM;
generator.Parameters.ImageHeight.Millimeters = heightInMM;
generator.Parameters.Resolution = 300;
// Save the barcode image
generator.Save("Textbarcode.png", Aspose.BarCode.Generation.BarCodeImageFormat.Png);
@GSA,
It’s good to hear that the suggested code segment meets your needs and that you were able to refine it for your specific scenario. Please don’t hesitate to reach out to us if you have any further questions or feedback.