【问题标题】:Printing a Barcode - Win 7 Works, Win 8 Blurry打印条码 - Win 7 Works, Win 8 Blurry
【发布时间】:2014-12-16 14:09:30
【问题描述】:

不久前我编写了一个程序来创建要在各种打印机上打印的条形码。最近有人在Windows 8上使用它,我遇到了以下问题:

  1. 打印最初是空白的。由于某种原因,我不得不重新计算/重新调整所有内容。
  2. 打印总是模糊的。模糊到无法扫描条形码。

知道为什么条形码可能会模糊吗?如果我打印到“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 设置。

标签: c# wpf printing


【解决方案1】:

正如 Matthew Watson 在 cmets 中所说,这是 Windows 8 驱动程序问题。我们的办公室在每台计算机上都安装了使用 Windows 驱动程序的网络打印机。我安装了特定打印机的驱动程序,并且模糊已修复。然而,这是不幸的,因为 Windows 7 打印机驱动程序可以工作。我们将不得不通知客户不要使用 Windows 8 驱动程序。

32 位计算机上的 Windows 7 驱动程序似乎也存在问题。这个是专门用于 Lexmark 打印机的,它会切断打印。奇怪的是,解决方案是在首选项中将纸张尺寸设置为 A4。我没有意识到印刷有多糟糕......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2014-07-11
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多