简体   繁体   中英

C# Generate barcode from datagridview to picturebox

Is it possible to generate a barcode from a column selected in DataGridView?

I want to show the ISBN as a barcode in the PictureBox在此处输入图片说明

Here is my code

private void GunaDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.EAN_13 };
  string txtBarcode = gunaDataGridView1[2, e.RowIndex].Value.ToString();
  picBarcode = writer.Write(txtBarcode);

  lblBarcode.Text = gunaDataGridView1[2, e.RowIndex].Value.ToString();
  lblBookTitle.Text = gunaDataGridView1[3, e.RowIndex].Value.ToString();
  lblISBN.Text = gunaDataGridView1[4, e.RowIndex].Value.ToString();
  lblCategory.Text = gunaDataGridView1[5, e.RowIndex].Value.ToString();
  lblGenre.Text = gunaDataGridView1[6, e.RowIndex].Value.ToString();
  lblMediaType.Text = gunaDataGridView1[7, e.RowIndex].Value.ToString();
  lblLanguage.Text = gunaDataGridView1[8, e.RowIndex].Value.ToString();
  lblAuthor.Text = gunaDataGridView1[9, e.RowIndex].Value.ToString();
  lblPublisher.Text = gunaDataGridView1[10, e.RowIndex].Value.ToString();
  lblPrice.Text = gunaDataGridView1[11, e.RowIndex].Value.ToString();
}

I am using ZXing.Net extension to generate a barcode.

I have tried

BarcodeWriter writer = new BarcodeWriter() { Format = BarcodeFormat.EAN_13 };
string txtBarcode = gunaDataGridView1[2, e.RowIndex].Value.ToString();
picBarcode = writer.Write(txtBarcode);

But it is giving me an error

Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Windows.Forms.PictureBox'

I have also tried the code

picBarcode = writer.Write(gunaDataGridView1[4, e.RowIndex].Value.ToString());

But it is still not working.

You can't set your PictureBox directly, you need to set its Image Property

picBarcode.Image = <your image> 

Likely

picBarcode.Image = writer.Write(gunaDataGridView1[4, e.RowIndex].Value.ToString());

Also remember to Dispose if you are setting it more than once (to clean up the GDI resources ), you can achieve that short-hand using the null conditional operator and the Dispose() method

picBarcode.Image?.Dispose();
picBarcode.Image = <Your Image>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM