如何将List 的值在将其编码为c#中的条形码后添加到List 中

我有两个List一个是字符串,另一个是PictureBox类型。 我想取List类型字符串的值,然后将其转换为条形码,然后将其保存到PictureBox类型的List中。

我现在这样做:

List PictureBoxList = new List(); List SerialNumberList = new List(); int SerialNumberStart = 0; for(int i = 0; i < 10 ; i++) { SerialNumberStart++; SerialNumberList.Add("S" + SerialNumberStart); } private void PrintButton_Click(object sender, EventArgs e) { for(int j =0 ; j < SerialNumberList.Count ; j++) { BarcodeLib.TYPE barcodetype1 = BarcodeLib.TYPE.CODE39; BarcodeLib.Barcode bar1 = new BarcodeLib.Barcode(); bar1.IncludeLabel = true; PictureBoxList[j].Image = bar1.Encode(barcodetype1 ,SerialNumberList[j]); // It gives me exception of Index out of range PictureBoxList.Add(PictureBoxList[j]); printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); printDocument1.Print(); } } private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Bitmap myBitmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.DrawToBitmap(myBitmap1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); e.Graphics.DrawImage(myBitmap1, 0, 0); myBitmap1.Dispose(); } 

我的第一个问题是如何将字符串转换为PictureBox。 然后将PictureBox的每个项目转换为Bitmap,然后打印所有位图,现在代码只打印一个条形码

在此处输入图像描述

你想要这样..对吧? 看到这是3of9中的这个字符串“S1253551”和纯文本的代表,最后是图像对吗?

  public Image stringToImage(string inputString) { string text = inputString.Trim(); Bitmap bmp = new Bitmap(1, 1); //Set the font style of output image Font font = new Font("Free 3 of 9", 25, FontStyle.Regular, GraphicsUnit.Pixel); Font font2 = new Font("Arial", 15, FontStyle.Regular, GraphicsUnit.Pixel); Graphics graphics = Graphics.FromImage(bmp); int width = (int)graphics.MeasureString(text, font).Width; int height = (int)graphics.MeasureString(text, font).Height; int height2 = (int)graphics.MeasureString(text, font2).Height; bmp = new Bitmap(bmp, new Size(width, height+height2)); graphics = Graphics.FromImage(bmp); //Specify the background color of the image graphics.Clear(Color.Cyan); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; //Specify the text, font, Text Color, X position and Y position of the image graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0); graphics.DrawString(text, font2, new SolidBrush(Color.Black), 0, height); graphics.Flush(); graphics.Dispose(); //if you want to save the image uncomment the below line. //bmp.Save(@"d:\myimage.jpg", ImageFormat.Jpeg); return bmp; } 

请记住,您必须安装“free of of 9”字体。

您传递字符串“S1253551”并生成条形码并在底部添加纯文本,最后将其作为图像返回。

它的工作代码我已经尝试过了。 请享用。 🙂

从这里下载工作代码下载

意思是你有字符串,你已将其转换为条形码。 最后我们在条形码中有一个属性,它保存了字符串的值吗? 现在你想将该字符串显示为图像??

若有,请参阅以下代码 –

  public Image stringToImage(string inputString) { string text = inputString.Trim(); Bitmap bmp = new Bitmap(1, 1); //Set the font style of output image Font font = new Font("Arial", 25, FontStyle.Regular, GraphicsUnit.Pixel); Graphics graphics = Graphics.FromImage(bmp); int width = (int)graphics.MeasureString(text, font).Width; int height = (int)graphics.MeasureString(text, font).Height; bmp = new Bitmap(bmp, new Size(width, height)); graphics = Graphics.FromImage(bmp); //Specify the background color of the image graphics.Clear(Color.Cyan); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.TextRenderingHint = TextRenderingHint.AntiAlias; //Specify the text, font, Text Color, X position and Y position of the image graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0); graphics.Flush(); graphics.Dispose(); //if you want to save the image uncomment the below line. //bmp.Save(@"d:\myimage.jpg", ImageFormat.Jpeg); return bmp; } 

我相信, 如果 bar1.Encode实际上有返回类型的Image ,这行在PrintButton_Click方法中:

 PictureBoxList[j].Image = bar1.Encode(barcodetype1 ,SerialNumberList[j]); // It gives me exception of Index out of range PictureBoxList.Add(PictureBoxList[j]); 

应该是这样的:

 var pictureBox = new PictureBox(); pictureBox.Image = bar1.Encode(barcodetype1 ,SerialNumberList[j]); PictureBoxList.Add(pictureBox); 

更新:

为了说清楚,我上面的答案意味着PrintButton_Click应该如下:

 private void PrintButton_Click(object sender, EventArgs e) { for(int j =0 ; j < SerialNumberList.Count ; j++) { BarcodeLib.TYPE barcodetype1 = BarcodeLib.TYPE.CODE39; BarcodeLib.Barcode bar1 = new BarcodeLib.Barcode(); bar1.IncludeLabel = true; var pictureBox = new PictureBox(); pictureBox.Image = bar1.Encode(barcodetype1 ,SerialNumberList[j]); PictureBoxList.Add(pictureBox); printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); printDocument1.Print(); } }