【发布时间】:2014-12-16 14:09:30
【问题描述】:
不久前我编写了一个程序来创建要在各种打印机上打印的条形码。最近有人在Windows 8上使用它,我遇到了以下问题:
- 打印最初是空白的。由于某种原因,我不得不重新计算/重新调整所有内容。
- 打印总是模糊的。模糊到无法扫描条形码。
知道为什么条形码可能会模糊吗?如果我打印到“Microsoft XPS Writer”,它看起来很好。
XPS 示例:
打印输出:
请注意,“Cats”打印输出正在打印 C***,因为我没有将代码 39 的小写字母自动大写。无论如何,Windows 7 打印输出仍然不模糊,而 Windows 8 打印输出非常模糊。
打印对话框/打印代码:
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;
}
【问题讨论】:
-
两者使用同一台打印机?
-
@MatthewWatson 是的。同一台打印机。
-
我猜这与打印机设置有关(或者 Windows 8 使用不同的驱动程序)。鉴于它在 XPS 中看起来不错,那么当您从 XPS 打印它时它看起来如何?
-
您也可以尝试在您显示条形码的任何
Image元素上将SnapsToDevicePixels属性设置为True。 -
SnapToDevicePixels 没有修复它。从 XPS 打印仍然模糊不清。我要弄乱一些 Windows 的东西,看看它是否只是某个地方的 Windows 8 设置。