Reputation: 8308
I wrote a program a while back to create barcodes to be printed on various printers. Recently, someone was using it on Windows 8. I've encountered the following issues:
Any ideas why the barcode might be blurred? If I print to the "Microsoft XPS Writer", it looks fine.
XPS Example:
Printout:
Note that the "Cats" printout is printing C*** because I didn't auto-uppercase the lowercase letters for code 39. Regardless, the Windows 7 printout is still not blurry whereas the windows 8 printout is very blurry.
Code for Print Dialog / Printing:
try
{
if (txtRow.Text.Trim() == "")
{
Popup.Message msg = new Message("Enter some text to print a barcode.");
msg.ShowDialog();
return;
}
PrintDialog dlg = new PrintDialog();
bool? result = dlg.ShowDialog();
if (result.HasValue && result.Value)
{
var Resolution = dlg.PrintTicket.PageResolution;
bmp = generator.Generate(txtRow.Text);
printImage.Stretch = Stretch.None;
printImage.UseLayoutRounding = false;
printImage.Source = loadBitmap(bmp); //transform.Clone();
printImage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
printImage.Arrange(new Rect(0, 0, printImage.Height, printImage.Width));
Grid main = new Grid();
main.Children.Add(printImage);
//get the size of the printer page
Size sz = new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight);
//update the layout of the visual to the printer page size.
main.Measure(sz);
Point ptGrid = new Point(0, -((dlg.PrintableAreaHeight / 2) - (printImage.ActualHeight / 2)));
main.Arrange(new Rect(ptGrid, sz));
//now print the visual to printer to fit on the one page.
dlg.PrintVisual(main, "Barcode 123");
}
}
catch (Exception er)
{
Globals.Variables.logger.Error(er);
Globals.Methods.ShowMessage("An unknown error occurred.");
}
Code for Generating the image:
public static BitmapSource loadBitmap(System.Drawing.Bitmap source)
{
IntPtr ip = source.GetHbitmap();
BitmapSource bs = null;
try
{
bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip,
IntPtr.Zero, Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(ip);
}
return bs;
}
public Bitmap Generate(string barcode, double scale = 1)
{
this.scale = scale;
// Width = 3 * wide + 6 * narrow + gap
int width = (int)Math.Ceiling((((3 * wide) + (6 * narrow) + gap) * (barcode.Count() + 2)) + (padding * 2));
int height = GetHeight();
left = (int)padding;
top = (int)padding;
barcode = barcode.ToUpper();
Bitmap bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(bmp))
using (SolidBrush black = new SolidBrush(Color.Black))
using (SolidBrush white = new SolidBrush(Color.White))
{
// Start the barcode:
addBar(gfx, black, white, '*');
foreach (char c in barcode)
{
addCharacter(gfx, black, white, c);
}
// End the barcode:
addBar(gfx, black, white, '*');
}
//bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
return bmp;
}
Upvotes: 1
Views: 607
Reputation: 8308
As was stated by Matthew Watson in comments, this is a Windows 8 driver issue. Our office has network printers installed using Windows drivers on each computer. I installed the drivers for the specific printer and the blurring was fixed. This is unfortunate, however, since Windows 7 printer drivers work. We will have to inform customers not to use Windows 8 drivers.
There appears to be an issue with the Windows 7 driver on a 32 bit computer as well. This one was specifically with Lexmark printers, where it would cut off the print. Oddly, the solution was to set the paper size to A4 in the preferences. I didn't realize just how bad printing was...
Upvotes: 1