如何添加 List<string> 的值进入列表<PictureBox>在 c# 中将其编码为 Barcode 后 [英] How to Add the values of List&lt;string&gt; into the List&lt;PictureBox&gt; after encoding it to Barcode in c#

查看:11
本文介绍了如何添加 List<string> 的值进入列表<PictureBox>在 c# 中将其编码为 Barcode 后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two List one is of string and the other is of PictureBox type. I want to take the values of List of type string and the convert it into the Barcode then save it to the List of type 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的每一项转换为Bitmap,然后打印所有的位图,现在代码只打印一个条码

My first question is that How can I convert the string into the PictureBox. And then converting each item of the PictureBox to the Bitmap and then printing all of the bitmaps now the code only Prints one barcode

推荐答案

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

you want like this.. right?? see this is the representaion of the this string "S1253551" in 3of9 and plain text and finally as image right??

    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"并生成条形码并在底部添加纯文本,最后将其作为图像返回.

Remember you must have installed "free 3 of 9" font.

you pass the string "S1253551" and it generate the barcode and add the plain text at bottom and finally return it as image.

我最后尝试过它的工作代码.享受.:)

Its working code i have tried at my end. Enjoy. :)

从这里下载工作代码下载

这篇关于如何添加 List<string> 的值进入列表<PictureBox>在 c# 中将其编码为 Barcode 后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆