VintaSoft Barcode .NET SDK Discussions
Questions, comments and suggestions concerning VintaSoft Barcode .NET SDK.
Board index < VintaSoft Barcode < VintaSoft Barcode .NET SDK Discussions
resolution = 72
textForBarcode = "8R27854156264";
width = 50; // in millimeters
height = 25; // in millimeters
public static Bitmap GetBarcode128(ParcelLabelOrientation parcelLabelOrientation, string textForBarcode, int resolution, int width, int height)
{
if (String.IsNullOrEmpty(textForBarcode))
{
return null;
}
Bitmap img = null;
// set barcode writer settings
BarcodeWriter barcodeWriter = new BarcodeWriter();
barcodeWriter.Settings.Resolution = resolution;
barcodeWriter.Settings.Barcode = BarcodeType.Code128;
barcodeWriter.Settings.SetHeight(height, UnitOfMeasure.Millimeters);
barcodeWriter.Settings.SetMinWidth(0.375, UnitOfMeasure.Millimeters);
barcodeWriter.Settings.Code128EncodingMode = Vintasoft.Barcode.BarcodeInfo.Code128EncodingMode.Undefined;
barcodeWriter.Settings.ValueVisible = false;
barcodeWriter.Settings.Value = textForBarcode;
// get a barcode image
img = barcodeWriter.GetBarcodeAsBitmap();
return img;
}
string filename = "test.png";
ImageFormat imageformat = ImageFormat.Png;
string barcodeValue = "8R27854156301";
int width = (50 / 10) / 2.54 * 72;
int height = (25 / 10) / 2.54 * 72;
BarcodeWriter writer = new BarcodeWriter();
writer.Settings.SetHeight(height - writer.Settings.ValueFont.Height * 2, UnitOfMeasure.Pixels);
writer.Settings.Barcode = BarcodeType.Code128;
writer.Settings.Value = barcodeValue;
using (GraphicsPath barcodePath = writer.GetBarcodeAsGraphicsPath())
{
using (Image image = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
using (Graphics g = Graphics.FromImage(image))
{
// draw barcode path
RectangleF barcodePathBounds = barcodePath.GetBounds();
g.Clear(Color.White);
float padding = 5;
using (Matrix m = new Matrix((width - padding * 2) / barcodePathBounds.Width, 0, 0, 1, padding, padding))
barcodePath.Transform(m);
// enable AntiAlias if need!
//g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(Brushes.Black, barcodePath);
// draw barcode value
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
RectangleF textRect = new RectangleF(
0,
barcodePathBounds.Height + writer.Settings.ValueGap,
width,
height - barcodePathBounds.Height - writer.Settings.ValueGap);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawString(contents, writer.Settings.ValueFont, Brushes.Black, textRect, sf);
}
}
image.Save(filename, imageformat);
}
Best regards, Alexander
0.375 mm = 0,01476378 inchAs I said you before our barcode writer uses 2 pixels as the minimum bar width for Code128 barcode. This was made because we do not recommend to use 1 pixels bars in Code128 barcodes if you want to create the stable barcode reading system. In your situation you need generate barcode in vector form (as a graphics path) and scale the vector as you want.
Resolution is 72 ppi, so the width of the smallest bar in pixels should be : 0,01476378 x 72 = 1,06299216 pixels
To generate such barcodes with Vintasoft, is it OK to use a MinWidth value of 1 ?