C# 打印条形码-Win 7工作,Win 8模糊
不久前,我写了一个程序来创建要在各种打印机上打印的条形码。最近,有人在Windows8上使用它。我遇到了以下问题:C# 打印条形码-Win 7工作,Win 8模糊,c#,wpf,printing,C#,Wpf,Printing,不久前,我写了一个程序来创建要在各种打印机上打印的条形码。最近,有人在Windows8上使用它。我遇到了以下问题: 印刷品最初是空白的。出于某种原因,我不得不检查并重新计算/重新调整所有内容 印刷品总是模糊不清。模糊到我无法扫描条形码的程度 你知道为什么条形码会模糊吗?如果我打印到“Microsoft XPS Writer”,它看起来很好 XPS示例: 打印输出: 请注意,“Cats”打印输出正在打印C***,因为我没有自动将代码39的小写字母大写。无论如何,Windows 7打印输出仍然不
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.");
}
生成图像的代码:
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;
}
正如Matthew Watson在评论中所述,这是Windows 8驱动程序的问题。我们的办公室在每台计算机上安装了使用Windows驱动程序的网络打印机。我为特定的打印机安装了驱动程序,模糊得到了修复。然而,这是不幸的,因为Windows7打印机驱动程序可以工作。我们必须通知客户不要使用Windows 8驱动程序
32位计算机上的Windows 7驱动程序似乎也存在问题。这一个是专门与利盟打印机,它会切断打印。奇怪的是,解决方案是在首选项中将纸张大小设置为A4。我没有意识到打印有多糟糕…使用同一台打印机打印两种东西?@MatthewWatson是的。同一台打印机。我猜这和打印机设置有关(或者Windows8使用了不同的驱动程序)。如果它在XPS中看起来正常,那么当您从XPS打印它时,它看起来如何?您还可以尝试在显示条形码的任何
图像元素上将SnapsToDevicePixels
属性设置为True
。SnapToDevicePixels没有修复它。XPS的打印仍然模糊不清。我要把一些Windows的东西弄得一团糟,看看它是否只是一个Windows8的设置。