I am generating some labels, with various barcodes. The structure is assembled from some controls previously located in a form.
psettings.PrinterName = MapPrinters.Printers.GetPrinter(this.form.printer.PrinterSettings.PrinterName);
pd.PrinterSettings.PrinterName = psettings.PrinterName;
pd.PrinterSettings.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler((object o, PrintPageEventArgs e) =>
{
for (int i = pageCount; i < data.Rows.Count; i++)
{
//e.Graphics.TranslateTransform(panel.Width/2,panel.Height/2,System.Drawing.Drawing2D.MatrixOrder.Append);
//Impresió en vertical
if (((CheckBox)Controls["chkPrint"]).Checked)
{
e.Graphics.RotateTransform(90,System.Drawing.Drawing2D.MatrixOrder.Append);
e.Graphics.TranslateTransform(291, 5, System.Drawing.Drawing2D.MatrixOrder.Append);
}
PrintLines(form, e);
lData.ForEach((x =>
{
e.Graphics.DrawString(GetCellData(x.Text, data.Rows[i], x), x.Font, Brushes.Black, ObtainRectangle(x,0,0,offsetY), Alineament(x));
}));
lLabel.ForEach((x =>
{
e.Graphics.DrawString(x.Text, x.Font, Brushes.Black, ObtainRectangle(x, 0, 0, offsetY), Alineament(x));
}));
lBarCode.ForEach((x =>
{
if (GenerateBarcode(x.Text, data.Rows[i], x) != null)
e.Graphics.DrawImage(GenerateBarcode(x.Text, data.Rows[i], x), ObtainRectangle(x, 5, 10,offsetY));
}));
if (panel != null)
e.Graphics.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(panel.Location.X, panel.Location.Y -offsetY, panel.Width, panel.Height));
if (pageCount == data.Rows.Count - 1)
e.HasMorePages = false;
else
{
e.HasMorePages = true;
pageCount++;
return;
}
}
});
form.printer.Document = pd;
try
{
// MessageBox.Show(pd.PrinterSettings.ToString());
if (((CheckBox)Controls["chkPrint"]).Checked)
pd.DefaultPageSettings.PaperSize = new PaperSize("label", 291, 826);
else
pd.DefaultPageSettings.PaperSize = new PaperSize("label", 826, 291);
pd.Print();
lcOK = true;
}
catch (Exception ex)
{
lcOK = false;
MessageBox.Show(ex.Message);
}
return lcOK;
The barcode is generated with the IronBarCod library
private Image GenerateBarcode(string text, DataRow dataRow, TextBox ctrl)
{
string toData = text.Replace('[', ' ').Replace(']', ' ').Trim();
if (dataRow.Table.Columns.Contains(toData))
{
if (dataRow[toData].ToString().Trim() != String.Empty)
{
try
{
return BarcodeWriter.CreateBarcode(dataRow[toData].ToString(), BarcodeEncoding.Code128).ToBitmap();
}
catch (Exception e)
{
return new Bitmap(1, 1);
}
}
else return null;
}
else return null;
}
although I have tried with BarCodeLib and GenCode128 with identical result.
The thing is that if I print by default in horizontal mode, everything works perfectly, the problem is that the print can be horizontal and vertical, for which I do a Graphics.rotatetransform(90) and this is when the label is printed correctly , but the barcode lines are printed a bit shaded, enough that the reader doesn't understand them anymore.
Can you give me a hand please?
Thank you very much in advance.
You have to put:
Or another value, this is the one that gave me the best result.
Without using this, I get the following: (note: all images have been zoomed 500%)
With InterpolationMode.NearestNeighbor
With InterpolationMode.HighQualityBicubic
I put it at the beginning of the PrintPageEventHandler event