我有一个PictureBox类型的数组。我想填写字符串列表,然后将其转换为条形码。但我是叔叔将字符串转换为PictureBox。有什么步骤可以让它们兼容吗?
System.Windows.Forms.PictureBox[] PictureBoxArray = new PictureBox[3];
List<string> serial = new List<string>;
public void ConvertToBarCode()
{
BarcodeLib.TYPE barcodetype1 = BarcodeLib.TYPE.CODE39;
BarcodeLib.Barcode bar1 = new BarcodeLib.Barcode();
bar1.IncludeLabel = true;
PictureBoxArray[0] = serial[0]; // Want to Convert String to PictureBox
PictureBoxArray[0].Image = bar1.Encode(barcodetype1, SerialNumberList[0]);
}
我已经用字符串填充了串行列表,现在只想转换。
答案 0 :(得分:1)
你想要这样..对吧?看到这是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;
}
传递字符串“S1253551”并生成条形码并在底部添加纯文本,最后将其作为图像返回。
我的工作代码我已经尝试过了。请享用。 :)
从这里下载工作代码Download