【问题标题】:C# barcode on image is too thick图像上的 C# 条码太粗
【发布时间】:2014-05-17 22:03:55
【问题描述】:

我创建了一个 Windows 窗体应用程序,我想生成一个带有条形码的交货单。我已嵌入条形码字体,但出现错误。看到这个问题:Embed Barcode in C# PDF Library

现在,我想用条形码制作一张图片,并将这张图片嵌入到我的送货单上。我在谷歌上搜索过这样做,我发现了以下代码:

    private Image DrawBarcodeAfleverbonImage(String text)
    {
        Font barcodeFont = new Font("Bar-Code 39", 31, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
        //Font barcodeFont = new Font("Arial", 31, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);

        //first, create a dummy bitmap just to get a graphics object
        Image img = new Bitmap(1, 1);
        Graphics drawing = Graphics.FromImage(img);

        //measure the string to see how big the image needs to be
        SizeF textSize = drawing.MeasureString(text, barcodeFont);

        //free up the dummy image and old graphics object
        img.Dispose();
        drawing.Dispose();

        //create a new image of the right size
        img = new Bitmap((int)textSize.Width, (int)textSize.Height);

        drawing = Graphics.FromImage(img);

        //create a brush for the text
        Brush textBrush = new SolidBrush(Color.Black);
        drawing.DrawString(text, barcodeFont, textBrush, 0, 0);

        drawing.Save();
        img.Save(@"C:\Users\Marten\Documents\test.png");

        textBrush.Dispose();
        drawing.Dispose();

        return img;

如果我运行我的程序,将创建一个图像。只有一个问题:条码字体太粗,无法扫描:

怎么了?

【问题讨论】:

  • 减小字体大小,它会工作...... 31到可能10?
  • 我试过了,但不起作用。现在我有一个小图像,全黑。

标签: c# winforms pdf drawing barcode


【解决方案1】:

您需要在绘制文本之前设置背景颜色:

drawing.Clear(Color.White);
drawing.DrawString(text, barcodeFont, textBrush, 0, 0);

或者如果你想要一个透明的背景,你需要关闭字体平滑。

drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
drawing.DrawString(text, barcodeFont, textBrush, 0, 0);

【讨论】:

  • 对不起,我的回复晚了,但它有效!谢谢;)
猜你喜欢
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多