2017-08-06 39 views
0

enter image description here帶有字符的禪宗條碼。 Winforms

嗨沒有人知道如何在底部包含數字/字符串時,它繪製條形碼?

這裏是我的代碼

 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回答。這裏是新的輸出。

enter image description here

但它我希望它看起來像這樣的條形碼重疊: enter image description here

謝謝

+2

如果你還不知道如何使用Graphics.FromImage(),然後保持簡單,把一個標籤的圖片框的下方。沒有必要將條形碼放入數據庫,只保存文本。 –

回答

0

可以與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; 
+0

先生它的工作,但數字是重疊的條形碼。等待病後張貼問題 – FutureDev

+0

@FutureDev,我更新了答案。 –

+0

謝謝你,先生。條形碼掃描儀是否可讀? – FutureDev

相關問題