【问题标题】:How to Add the values of List<string> into the List<PictureBox> after encoding it to Barcode in c#如何在 c# 中将 List<string> 的值编码为 Barcode 后将其添加到 List<PictureBox> 中
【发布时间】:2014-02-10 13:05:46
【问题描述】:

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

我现在就是这样:

List<System.Windows.Forms.PictureBox> PictureBoxList = new List<System.Windows.Forms.PictureBox>();
List<string> SerialNumberList = new List<string>();
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 的每个项目转换为位图,然后打印所有位图,现在代码只打印一个条形码

【问题讨论】:

  • PrintButton_Click 方法中,您使用j 作为循环计数器变量,但在循环内的所有代码中使用i 而不是j。错字还是...?
  • 抱歉编辑了我的代码@har07

标签: c# printing bitmap barcode 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

【讨论】:

  • 我可以将此值设置为 PictureBox 还是只需要制作一个 ImageList?
  • 当我点击打印按钮时它不打印它给我空白页
  • nosheen 您必须为特定或所有您想要的字符串调用此方法。它会为您返回一张图片,然后对这张图片做任何您想做的事情。由你决定,你想显示销毁保存或任何你想要的。
  • 是的,它向我展示了 PictureBox 中的图像,但我如何在页面上打印它? :/真的很困惑
  • 我已经为你创建了示例,但这就是我的意思是窗口表单尝试提供字符串并单击以生成它.. 它的列表基于你想要的我将在我的谷歌文档中上传并分享与你链接..
【解决方案2】:

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

如果是这样,请参考以下代码 -

    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;
    }

【讨论】:

  • 是的,但实际上我有一个字符串列表,对于每个字符串,我想创建一个条形码,然后将其保存到图片框列表中,然后将图片框的每个元素传递给位图。如何将每个元素的宽度和高度传递给位图构造函数?
  • 我很抱歉,但我不知道你所说的“创建条形码.....”是什么意思。我可以告诉你如何从字符串列表中创建具有相同图像的图像列表或图像框列表 - 字符串列表 -> 字符串图像列表列表 -> 图片框列表
  • 是的,我想要你所说的
  • 你的意思是你想要字符串列表中的图像列表对吗?那么你也可以使用上面的代码..为列表中的每个字符串调用上面的函数或更改接受字符串列表并返回图像列表的函数本身。
  • 但是我想要条形码格式的图像,我使用的是编码方法
【解决方案3】:

我相信,如果 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();
   }
}

【讨论】:

  • 第一个会给出索引超出范围异常,因为我也试过了,如果我知道 PictureBox 的数量,第二个会正常工作,但就我而言,我不知道我需要很多 PictureBox,所以想让它动态
  • 我的意思是,您需要用第二个代码块替换部分代码(显示在我的答案的第一个代码块中)。无需知道图片框的数量..
  • 我已经使用了您的代码,但现在我必须将其打印到页面上,为此我必须将其转换为位图并传递每个 PictureBox 的宽度和高度。
猜你喜欢
  • 2012-12-26
  • 1970-01-01
  • 2021-06-26
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
相关资源
最近更新 更多