【问题标题】:How to convert string to a PictureBox in C# [duplicate]如何在 C# 中将字符串转换为 PictureBox [重复]
【发布时间】:2014-01-21 06:14:24
【问题描述】:

我有一个 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]);
}

我已经用字符串填充了序列列表,现在只想要转换。

【问题讨论】:

  • 即使它已编译,您希望该行做什么?当您将字符串转换为图片框时,您期望会发生什么?
  • 你可以看看可能会质疑它可以帮助stackoverflow.com/questions/20244595/…
  • 我首先必须填充 PictureBox 数组,然后更改其 Image 属性。
  • 我们知道,但刺痛看起来如何?
  • 我的字符串看起来像这样 "S1101243" 现在我使用 Encode 方法获取字符串并将其传递给 PictureBox 的 Image 属性,就像这样 PictureBoxArray[0].Image = bar1.Encode(barcodetype1 , S1101243);但在此之前我必须将值传递给 PictureBox[0] 否则它会给出 NullReferenceException .. 而我的实际问题是这个 stackoverflow.com/questions/21228198/…

标签: c# string list picturebox


【解决方案1】:

你想要这样..对吗?看到这是这个字符串“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;
}

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

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

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

从这里下载工作代码Download

【讨论】:

  • 显然 OP 已经有一个条形码库。您的解决方案可能有效,但与原始代码相去甚远,不再使用条形码库并由新库(本例中为字体)交换。
  • @ThomasW。在 2-3 的地方发布了同样的问题,我问过他。他要我使用这个免费的 3 of 9 字体。它解释了我们如何从两个字符串创建图像。如果您想使用另一个库,则由您决定。您只需要修改上面代码中的 2-3 行。请注意。
  • Deepak:非常感谢你的这堂课。我正在使用它来显示零件 ID 号的条形码表示,但我无法使用字体 39 将图像放入 PictureBox。顺便说一句,我已经在其他项目中使用“免费 3 of 9”下载,并且效果很好;正如其他人建议的那样,我没有看到使用它的任何问题。顺便说一句,我建议您不要在 BackgroundImageLayout 属性上使用 ImageLayout.Stretch,而是使用“Center”和更大的字体大小。拉伸会导致扫描仪试图读取可能模糊的图像时出现问题。再次感谢。 --蒂姆
猜你喜欢
  • 2016-08-11
  • 2015-10-15
  • 2015-09-21
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2010-10-05
  • 2014-05-28
相关资源
最近更新 更多