barcodelib
barcodelib copied to clipboard
font not read
I have two bugs fond:
- you must have: b.AlternateLabel = "" null give an error about null exeption
(RawData.StartsWith(AlternateLabel) || (AlternateLabel == null) startWith go first after the null check I cann't by past beter
- you name size of 100 by 100 ean 13 and genereate label.
you cann't read the first nummer of barcode label
problem is by more sizes.
example on this sreentshot

Thanks
Is possible solution to problem to solve it: first number before the barcode lines: What you can see here:

Possible solution could be printing the Numbers above the Barcode Layer and add some White Space at starting and ending of the Numbercode. Or the solution you showed but I never saw that on an barcode or in an Barcode lib in my life.
I had in my previous work a fork solving some problems with the resolution, maybe setting the Image Resolution to 300 px could solve it. Unfortunately i quit before getting my changes out.
The problem with just adding arbitrary space before and after the barcode is that the consumer has specified a specific width for the barcode image to be and adding space for the number is either going to:
- increase this beyond what is specified to accommodate this space.
- encroach on the space to draw the barcode and shrink the barcode width itself more.
Im not sure which or if either is really acceptable. Just trying to weigh options here.
Sorry for late response. best is to take up space including for numbers such as size. so space becomes smaller. These belong to the barcode. Behelva if one chooses not to mention text. @LordPinhead have you a fixe?

@teunlielu, what you used to generate that barcode is also not correct. There should be a spacing after the 8.
The above code was generated with a font where the bars and spaces are defined as separate glyphs, together with the OCR font digit below it. @barnhill has made a library for generating pixel based images, not vector based, so any text (vector based) used as human readable text below the bars and spaces will probably never be perfect.

@rob313663 how would you solve? Vectorbase is not option. I think so. Is better with space but to smalle for hunan for reading.
@teunlielu Personally I already solved it. I made a font, exactly following the specifications of the EAN/UPC codes. I did that in 1997. Those fonts are not open source so I can't give it to you, we even stopped selling it because it generated too much support (never give a sharp knife to a child, I got a sharp knife, still have a scar to remind me).
barcodelib is for generating images of barcodes, the machine readable part, which I think it does in a very good way.
Making the human readable part in barcodes, especially UPC/EAN is very hard unless you go for a font, which barcodelib is not.
@teunlielu And I am not a developer of barcodelib, or even a contributor, just trying to pitch in with my knowledge of barcodes.
@teunlielu will something like this do?
var b = new BarcodeLib.Barcode();
var i = b.Encode(BarcodeLib.TYPE.EAN13, "8901072002478");
Image resultImage = new Bitmap(i.Width+200, i.Height+50, PixelFormat.Format24bppRgb);
Font font = new Font("Courier New", 26 * 1.33f, FontStyle.Regular, GraphicsUnit.Pixel);
SolidBrush drawBrush = new SolidBrush(Color.Black);
float x = 100f - 35f;
float y = i.Height;
string humanReadable = "8 901072 002478";
using (Graphics g = Graphics.FromImage(resultImage))
{
g.FillRectangle( Brushes.White, 0, 0, resultImage.Width, resultImage.Height);
g.DrawImage(i, 100, 0);
g.DrawString(humanReadable, font, drawBrush, x, y);
}
resultImage.Save(@"C:\temp\ean13.png", ImageFormat.Png);

I have works at the moment old versions this lib. Give this same output of your. New idea of barcode format is beautyfuller. But old works for me fine. We have two options change code render old version or is possible to fix this bug.
Is there a solution for second bug?
The label logic needs some work Im afraid and all barcode types have different label formats
My suggestion is:
change 0,5f to 0,72f in follow code then you end up with dashes above just 100%, but I think this is fairly easy to adjust @barnhill ?
/// <summary>
/// Draws Label for EAN-13 barcodes
/// </summary>
/// <param name="img">Image representation of the barcode without the labels</param>
/// <returns>Image representation of the barcode with labels applied</returns>
public static Image Label_EAN13(Barcode Barcode, Bitmap img)
{
try
{
int iBarWidth = Barcode.Width / Barcode.EncodedValue.Length;
string defTxt = Barcode.RawData;
using (Font labFont = new Font("Arial", getFontsize(Barcode, Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length, img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel))
{
int shiftAdjustment;
switch (Barcode.Alignment)
{
case AlignmentPositions.LEFT:
shiftAdjustment = 0;
break;
case AlignmentPositions.RIGHT:
shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length);
break;
case AlignmentPositions.CENTER:
default:
shiftAdjustment = (Barcode.Width % Barcode.EncodedValue.Length) / 2;
break;
}//switch
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(img, (float)0, (float)0);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat f = new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near
};
int LabelY = 0;
//Default alignment for EAN13
LabelY = img.Height - labFont.Height;
f.Alignment = StringAlignment.Near;
float w1 = iBarWidth * 4; //Width of first block
float w2 = iBarWidth * 42; //Width of second block
float w3 = iBarWidth * 42; //Width of third block
float s1 = shiftAdjustment - iBarWidth;
float s2 = s1 + (iBarWidth * 4); //Start position of block 2
float s3 = s2 + w2 + (iBarWidth * 5); //Start position of block 3
//Draw the background rectangles for each block
using (SolidBrush backBrush = new SolidBrush(Barcode.BackColor))
{
g.FillRectangle(backBrush, new RectangleF(s2, (float)LabelY, w2, (float)labFont.Height));
g.FillRectangle(backBrush, new RectangleF(s3, (float)LabelY, w3, (float)labFont.Height));
}
//draw datastring under the barcode image
using (SolidBrush foreBrush = new SolidBrush(Barcode.ForeColor))
{
using (Font smallFont = new Font(labFont.FontFamily, labFont.SizeInPoints * 0.72f * Barcode.DotsPerPointAt96Dpi, labFont.Style, GraphicsUnit.Pixel))
{
g.DrawString(defTxt.Substring(0, 1), smallFont, foreBrush, new RectangleF(s1, (float)img.Height - (float)(smallFont.Height * 0.9), (float)img.Width, (float)labFont.Height), f);
}
g.DrawString(defTxt.Substring(1, 6), labFont, foreBrush, new RectangleF(s2, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
g.DrawString(defTxt.Substring(7), labFont, foreBrush, new RectangleF(s3 - iBarWidth, (float)LabelY, (float)img.Width, (float)labFont.Height), f);
}
g.Save();
}
}//using
return img;
}//try
catch (Exception ex)
{
throw new Exception("ELABEL_EAN13-1: " + ex.Message);
}//catch
}//Label_EAN13`


