打印条形码 - 赢取7个作品,赢得8个模糊

时间:2014-12-16 14:09:31

标签: c# wpf printing

我一段时间写了一个程序来创建条形码,以便在各种打印机上打印。最近,有人在Windows 8上使用它。我遇到了以下问题:

  1. 印刷品最初是空白的。由于某种原因,我不得不通过并重新计算/重新调整所有内容。
  2. 打印件始终模糊。模糊到我无法扫描条形码。
  3. 为什么条形码可能会模糊?如果我打印到" Microsoft XPS Writer",它看起来很好。

    XPS示例: enter image description here

    打印结果: enter image description here

    请注意" 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;
    }
    

1 个答案:

答案 0 :(得分:1)

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

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