4

在此处输入图像描述

嗨,有人知道如何在绘制条形码时在底部包含数字/字符串吗?

这是我的代码

     private void btnGenerate_Click_1(object sender, EventArgs e)
    {
        Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
        pictureBox1.Image = barcode.Draw(textBox1.Text, 50);
    }

PS我应该将它保存在数据库列中并在那里调用它吗?谢谢

VVatashi 先生回答的更新基础。这是新的输出。

在此处输入图像描述

但它与条形码重叠,我希望它看起来像这样: 在此处输入图像描述

谢谢

4

1 回答 1

11

根据您的代码,您可以使用 System.Drawing 在图像上打印文本:

Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
var image = barcode.Draw(textBox1.Text, 50);

using (var graphics = Graphics.FromImage(image))
using (var font = new Font("Consolas", 12)) // Any font you want
using (var brush = new SolidBrush(Color.White))
using (var format = new StringFormat() { LineAlignment = StringAlignment.Far }) // To align text above the specified point
{
    // Print a string at the left bottom corner of image
    graphics.DrawString(textBox1.Text, font, brush, 0, image.Height, format);
}

pictureBox1.Image = image;

有点不清楚数据库与您问题的第一部分的关系。

更新。 哦,我没有注意到生成的条形码图是整个图像。在这种情况下,您可以在较大的图像上绘制条形码和文本:

Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;
var barcodeImage = barcode.Draw(textBox1.Text, 50);

var resultImage = new Bitmap(barcodeImage.Width, barcodeImage.Height + 20); // 20 is bottom padding, adjust to your text

using (var graphics = Graphics.FromImage(resultImage))
using (var font = new Font("Consolas", 12))
using (var brush = new SolidBrush(Color.Black))
using (var format = new StringFormat()
{
    Alignment = StringAlignment.Center, // Also, horizontally centered text, as in your example of the expected output
    LineAlignment = StringAlignment.Far
})
{
    graphics.Clear(Color.White);
    graphics.DrawImage(barcodeImage, 0, 0);
    graphics.DrawString(textBox1.Text, font, brush, resultImage.Width / 2, resultImage.Height, format);
}

pictureBox1.Image = resultImage;
于 2017-08-06T14:10:35.940 回答