I have a windows application that print serial numbers in 3 of 9 bar code font. It is then scanned by RF scanners. The issue is I am not able to scan certain serial numbers like if the serial has number 8, it is not scanning. Any advise would be appreciated!
Font barFont = new Font("3 of 9 Barcode", 10);
Graphics g = ev.Graphics;
g.PageUnit = GraphicsUnit.Millimeter;
SolidBrush br = new SolidBrush(Color.Black);
RectangleF rect = new RectangleF(new PointF(15, 2), new SizeF(45, 4));
RectangleF rect1 = new RectangleF(new PointF(15, 5), new SizeF(45, 3));
RectangleF rect2 = new RectangleF(new PointF(15, 7), new SizeF(45, 4));
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
g.DrawString("*" + printSerial + "*", barFont, br, rect, sf);
The issue was with the bar code font installed in my system fonts. Once I refreshed with the latest version, the bar codes are scanning.
Related
I 'm using the code below to print the barcode that gets generated in my C# WinForms, but the barcode scanner does not detect it, I've tried using code 128 font and code39 fonts with no luck, when I print using the bartender software it detects the barcode generated, just won't detect mine
here is the code
private void txtPprice_TextChanged(object sender, EventArgs e)
{
string barcode = txtCode.Text;
string price = txtPprice.Text;
string pname = txtPname.Text;
Bitmap bitm = new Bitmap(barcode.Length * 30, 90);
using (Graphics graphic = Graphics.FromImage(bitm))
{
Font newfont = new Font("IDAutomationHC39M", 10);
Font newfont2 = new Font("Arial Black", 8);
PointF point = new PointF(10f, 10f);
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush white = new SolidBrush(Color.White);
graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
graphic.DrawString("*" + barcode + "*", newfont, black, point);
PointF pointPrice = new PointF(45f, 55f);
graphic.DrawString("" + pname +"", newfont2, black, pointPrice);
PointF pointPname = new PointF(90f, 75f);
graphic.DrawString("" + price + " L.E.", newfont2, black, pointPname);
PointF pointBcode = new PointF(20f, 75f);
graphic.DrawString("" + barcode + "", newfont2, black, pointBcode);
}
using (MemoryStream Mmst = new MemoryStream())
{
bitm.Save("ms", ImageFormat.Jpeg);
pictureBox1.Image = bitm;
pictureBox1.Width = bitm.Width;
pictureBox1.Height = bitm.Height;
}
}
I have tried adding and removing the * as suggested in other posts, also changing the font size from 8p went one by one till 28px, but still no luck
the scanner and the printer are working fine on the bartender application
The code used in the example below is 5094411
Here is an image, the textbox highlighted in red is where the string barcode is coming from
and you can see in the image that in the picturebox it shows 5094411 in both IDautomationHC39M font (the barcode font) and again below it in Arial
This is the correct code that after some tries with the help of Jimi here I was able to get this part right
string Barcode = "*"+txtCode.Text+"*";
string price = txtPprice.Text;
string pname = txtPname.Text;
using (Bitmap bitmap = new Bitmap(350, 220))
{
bitmap.SetResolution(240, 240);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Font font = new Font("IDAutomationHC39M", 10, FontStyle.Regular, GraphicsUnit.Point);
graphics.Clear(Color.White);
StringFormat stringformat = new StringFormat(StringFormatFlags.NoWrap);
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
graphics.TextContrast = 10;
SolidBrush black = new SolidBrush(Color.Black);
SolidBrush white = new SolidBrush(Color.White);
PointF TextPosition = new PointF(45F, 10F);
SizeF TextSize = graphics.MeasureString(Barcode, font, TextPosition, stringformat);
PointF pointPrice = new PointF(90f, 125f);
Font newfont2 = new Font("Cambria", 8, FontStyle.Regular, GraphicsUnit.Point);
Font newfont3 = new Font("Arial Black", 10, FontStyle.Regular, GraphicsUnit.Point);
graphics.DrawString("" + pname + "", newfont3, black, pointPrice);
PointF pointPname = new PointF(200f, 170f);
graphics.DrawString("" + price + " L.E.", newfont3, black, pointPname);
PointF pointBcode = new PointF(35f, 170f);
graphics.DrawString("" + Barcode + "", newfont2, black, pointBcode);
if (TextSize.Width > bitmap.Width)
{
float ScaleFactor = (bitmap.Width - (TextPosition.X / 2)) / TextSize.Width;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.ScaleTransform(ScaleFactor, ScaleFactor);
}
graphics.DrawString(Barcode, font, new SolidBrush(Color.Black), TextPosition, StringFormat.GenericTypographic);
bitmap.Save(#"barcode.png", ImageFormat.Png);
this.pictureBox1.Image = (Bitmap)bitmap.Clone();
font.Dispose();
}
}
I have faced underline missing issue with if I only draw text (only spaces) with underline style. Please refer the below tried code at my end and let me know the solution to resolve this.
Bitmap bitmap = new Bitmap(400, 200);
Graphics graphics = Graphics.FromImage(bitmap);
Brush brush = new SolidBrush(Color.White);
graphics.FillRectangle(brush, 0, 0, 400, 200);
System.Drawing.Font font = new System.Drawing.Font("Arial", 12, FontStyle.Underline);
brush = new SolidBrush(Color.Black);
StringFormat stringformat = new StringFormat(StringFormat.GenericTypographic);
stringformat.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
string text = "Hello";
SizeF sizeF = m_graphics.MeasureString(text, font, new PointF(0, 0), stringformat);
graphics.DrawString(text, font, brush, new RectangleF(0, 0, sizeF.Width, sizeF.Height), stringformat);
text = " ";
float width = sizeF.Width;
sizeF = m_graphics.MeasureString(text, font, new PointF(0, 0), stringformat);
graphics.DrawString(text, font, brush, new RectangleF(width, 0, sizeF.Width, sizeF.Height), stringformat);
text = "World";
width += sizeF.Width;
sizeF = m_graphics.MeasureString(text, font, new PointF(0, 0), stringformat);
graphics.DrawString(text, font, brush, new RectangleF(width, 0, sizeF.Width, sizeF.Height), stringformat);
As far as I can see you have three options:
Use a monospaced font (Courier New and Lucida Sans Typewriter). More info on the monospaced fonts here and here.
System.Drawing.Font font =
new System.Drawing.Font("Courier New", 12, FontStyle.Underline);
Write the text at once. If you only write the spaces then the method won't work, even if you use TextRenderer to draw the string. So if you receive the strings separately then I suggest add them in a StringBuilder and draw the whole text or sentence.
var sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" ");
sb.Append("World!");
var bitmap = new Bitmap(400, 200);
var graphics = Graphics.FromImage(bitmap);
Brush brush = new SolidBrush(Color.White);
graphics.FillRectangle(brush, 0, 0, 400, 200);
var font = new Font("Arial", 12, FontStyle.Underline);
brush = new SolidBrush(Color.Black);
var stringformat = new StringFormat(StringFormat.GenericTypographic);
stringformat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
stringformat.Trimming = StringTrimming.None;
var text = sb.ToString();
var sizeF = graphics.MeasureString(text, font, new PointF(0, 0), stringformat);
graphics.DrawString(text, font, brush,
new RectangleF(5, 0, sizeF.Width, sizeF.Height), stringformat);
The hack version: You can draw an invisible character such as (char)127 which is the delete character, like this (you can use the code from point 2 and add this line when initializing the StringBuilder):
sb.Append(new string ((char)127, 5)); //this will create approx. five spaces.
You can use other invisible characters if you need.
The 3rd options is a hack and should be considered as such, I would recommend option 1 if you can change the font otherwise option 2.
This question already has answers here:
How to set line spacing Graphics.DrawString
(2 answers)
Closed 6 years ago.
CodeA:
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
graphics.DrawString( stringB,
new Font("Tahoma", 14), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
CodeB:
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n"+stringB,
new Font("Tahoma", 14, FontStyle.Underline), Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
I need to draw 2 string within a box. StringA with underline style while StringB don't.
CodeB almost achieve what I want but stringA and stringB sharing the same style. So I tested with CodeA but the program with it is that both string overlapping each other. May I know
The problem with codeA is that both stringA and stringB are drawn at the exact same position.
graphics.DrawString turns string into image and print it on the paper.
"\n" doesn't have any meaning once the string is turned into image. It will not be printed nor will it create a new line. In fact, there is no "lines" on the paper. Just image.
You need to give stringB different position. Use Graphics.MeasureString (String, Font) to measure the size of stringA, then adjust stringB's position according to the result.
Image imageChipsetName = new System.Drawing.Bitmap(photoWidth, photoHeight);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Font strFontA = new Font("Tahoma", 14, FontStyle.Underline);//Font used by stringA
Graphics graphics = Graphics.FromImage(imageChipsetName);
graphics.DrawString(stringA + "\n",
strFont_A, Brushes.Black,
new RectangleF(0, 0, photoWidth, photoHeight), strFormat);
SizeF stringSizeA = new SizeF();
stringSizeA = Graphics.MeasureString(stringA, strFont_A);//Measuring the size of stringA
graphics.DrawString(stringB,
new Font("Tahoma", 14), Brushes.Black,
new RectangleF(0, stringSizeA.Height, photoWidth, photoHeight - stringSizeA.Height), strFormat);
I would print rectangle in specified position from c# application.
For example 1 centimeter from the left edge of sheet and 1 centimeter from the top edge of sheet.
I tried something like this:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Brush brush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(Color.Black, 1);
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.PageSettings.Margins = new Margins(10, 10, 10, 10);
Rectangle rect = new Rectangle(10, 10, 50, 90);
e.Graphics.DrawRectangle(blackPen, rect);
}
But it does not work correctly.
This seems to be a very helpful discussion of the whole matter.
If you want your margins to work you need to change the PrintDocument.OriginsAtMargins from its default (false) to true.
As you don't seem to do that, your origin will be at the printable area , which is printer-dependent and seems to be at (3,2)mm for your printer. A simple test for this would be to print to a PDF printer, which ought to have the full page as its printable area; so it should appear at the top left of its page.
So if you add printDocument.OriginAtMargins = true; to maybe the BeginPrint event (or whereever you set up th page) and then print the rectangle to (0,0) it should appear at the margins you have set..
I change void to :
printDocument1.OriginAtMargins = true;
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(Color.Black, 1);
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.PageSettings.Margins = new Margins(0, 0, 0, 0);
Rectangle rect = new Rectangle(10, 10, 20, 20);
e.Graphics.DrawRectangle(blackPen, rect);
Now on print I have rectangle in 13 milimeter from the left edge of sheet and 12 milimeter from the top edge of sheet, but when I change code to :
printDocument1.OriginAtMargins = true;
Graphics g = e.Graphics;
Brush brush = new SolidBrush(Color.Black);
Pen blackPen = new Pen(Color.Black, 1);
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.PageSettings.Margins = new Margins(0, 0, 0, 0);
Rectangle rect = new Rectangle(10, 10, 20, 20);
e.Graphics.DrawRectangle(blackPen, rect);
and print to pdf printer I have rectango on 0,0 on sheet...
I am generating ICards for employees.
I have to write address of the employee on the ICard.
Image blankICard = Image.FromFile(#"C:\Users\admin\Pictures\filename.jpg");
Bitmap outputImage = new Bitmap(blankICard.Width, blankICard.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
System.Drawing.SolidBrush b = new SolidBrush(Color.FromArgb(255, 88, 89, 91));
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawImage(blankICard, new Rectangle(0, 0, blankICard.Width, blankICard.Height),
new Rectangle(new Point(), blankICard.Size), GraphicsUnit.Pixel);
Font stringFont = new Font("FreightSans Medium", 20, FontStyle.Regular);
string address = "Address goes here";
graphics.DrawString(address, new Font("FreightSans Medium", 20, FontStyle.Regular), b, new Point(621, 234));
graphics.DrawString("Employee Code:12345678", new Font("FreightSans Medium", 26, FontStyle.Regular), b, new Point(350, 407));
}
Current Output is shown in the left side of the image.
Here what happens that my string goes out of the box.
I want to bind it in the fix size box.
Example is shown in the right side of the Image.
Use Graphics.DrawString overload that takes Rectangle instead of a point. That way you'll wrap text to fit within specified width.
using (Graphics graphics = Graphics.FromImage(outputImage)){
// Draw whatever you need first
// ....
// Create font...
graphics.DrawString(employeeCode, font, Brushes.Black,
new Rectangle(0, 25, maxWidth, maxHeight);
}
Simple as that :)
I made some changes to your code, commenting 2 lines - I did not have the file C:\Users\admin\Pictures\filename.jpg on my pc - That's why blankICard was disabled and so was its Rectangle:
You must set maxWidth in order to wrap your employee code, for example.
// Image blankICard = Image.FromFile(#"C:\Users\admin\Pictures\filename.jpg");
int width = 500;
int height = 500;
Bitmap outputImage = new Bitmap(width, height,PixelFormat.Format32bppArgb);
SolidBrush b = new SolidBrush(Color.FromArgb(255, 88, 89, 91));
SolidBrush blackBrush = new SolidBrush(Color.Black);
using (Graphics graphics = Graphics.FromImage(outputImage))
{
graphics.DrawRectangle(new Pen(blackBrush), new Rectangle(0, 0, width, height));
// new Rectangle(new Point(), blankICard.Size), GraphicsUnit.Pixel);
Font stringFont = new Font("FreightSans Medium", 20, FontStyle.Regular);
string address = "Address goes here";
string employeeCode = "Employee Code:12345678";
int maxWidth = 30;
SizeF sf = graphics.MeasureString(employeeCode, new Font(new FontFamily("FreightSans Medium"), 26), maxWidth);
graphics.DrawString(address, new Font("FreightSans Medium", 20, FontStyle.Regular), b, new Point(0, 0));
graphics.DrawString(employeeCode, new Font(new FontFamily("FreightSans Medium"), 26), Brushes.Black,new RectangleF(new PointF(0, 25), sf),StringFormat.GenericTypographic);
//graphics.DrawString(, new Font("FreightSans Medium", 26, FontStyle.Regular), b, new Point(10, 20));
}