C# 如何在C中将字符串转换为PictureBox#

C# 如何在C中将字符串转换为PictureBox#,c#,string,list,picturebox,C#,String,List,Picturebox,我有一个PictureBox类型的数组。我想填写字符串列表,然后将其转换为条形码。但是我是把字符串转换成图片盒的叔叔。我可以采取什么措施使它们兼容 System.Windows.Forms.PictureBox[] PictureBoxArray = new PictureBox[3]; List<string> serial = new List<string>; public void ConvertToBarCode() { BarcodeLib.TYPE

我有一个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]);
}
System.Windows.Forms.PictureBox[]PictureBoxArray=new PictureBox[3];
列表序列=新列表;
public void ConvertToBarCode()
{
BarcodeLib.TYPE barcodetype1=BarcodeLib.TYPE.CODE39;
BarcodeLib.Barcode bar1=新的BarcodeLib.Barcode();
bar1.IncludeLabel=true;
PictureBoxArray[0]=串行[0];//要将字符串转换为PictureBox吗
PictureBoxArray[0]。Image=bar1.Encode(barcodetype1,SerialNumberList[0]);
}
我已经用字符串填充了序列列表,现在只需要转换。

你想要这样。。正确的??请看这是这个字符串“S1253551”在3of9和纯文本中的表示,最后是图像右

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;
}
请记住,您必须已安装“免费3/9”字体。 您传递字符串“S1253551”,它生成条形码并在底部添加纯文本,最后将其作为图像返回

它的工作代码我已经在我的末端尝试过了。享受。:)


从这里下载工作代码

您希望该行做什么,即使它已编译?当您将字符串转换为picturebox时,您预计会发生什么情况?您可能会怀疑它是否有帮助我首先必须填充picturebox数组,然后更改其图像属性。我们知道,但是sting的外观如何?我的字符串看起来像“S1101243”现在,我使用Encode方法获取字符串并将其传递给PictureBoxArray[0]这样的PictureBoxArray的Image属性;但在此之前,我必须将值传递给PictureBox[0],否则它将给出NullReferenceException。。我的实际问题是,OP显然已经有了条形码库。您的解决方案可能有效,但与原始代码相差甚远,不再使用条形码库,而是由新库(本例中的字体)进行交换。@ThomasW。我把同样的问题贴在2-3的地方,我问过他。他想让我用这个免费的3/9字体。这就是我们如何从两个字符串创建图像的解释。如果你想使用另一个lib,它取决于你。您只需要修改上面代码中的2-3行。迪帕克:非常感谢你的课。我用它来显示零件ID号的条形码表示,我用字体39将图像输入到图片框时遇到了问题。顺便说一句,我已经在为其他项目使用“9个免费3个”下载,它工作得非常好;我不认为像其他人建议的那样使用它有任何问题。顺便说一句,我建议你不要使用ImageLayout。扩展BackgroundImageLayout属性,改为使用“Center”和更大的字体大小。拉伸会导致扫描仪试图读取可能模糊的图像时出现问题。再次感谢--提姆