Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何添加列表的值<;字符串>;进入列表<;PictureBox>;将其编码为c中的条形码后#_C#_Printing_Bitmap_Barcode_Picturebox - Fatal编程技术网

C# 如何添加列表的值<;字符串>;进入列表<;PictureBox>;将其编码为c中的条形码后#

C# 如何添加列表的值<;字符串>;进入列表<;PictureBox>;将其编码为c中的条形码后#,c#,printing,bitmap,barcode,picturebox,C#,Printing,Bitmap,Barcode,Picturebox,我有两个列表,一个是字符串,另一个是PictureBox类型。我想获取字符串类型列表的值,并将其转换为条形码,然后将其保存到PictureBox类型列表中 我现在就是这样做的: List<System.Windows.Forms.PictureBox> PictureBoxList = new List<System.Windows.Forms.PictureBox>(); List<string> SerialNumberList = new List<

我有两个列表,一个是字符串,另一个是PictureBox类型。我想获取字符串类型列表的值,并将其转换为条形码,然后将其保存到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();
}
List PictureBoxList=new List();
List SerialNumberList=新列表();
int SerialNumberStart=0;
对于(int i=0;i<10;i++)
{
SerialNumberStart++;
SerialNumberList.Add(“S”+SerialNumberStart);
}
私有无效打印按钮\单击(对象发送者,事件参数e)
{
for(int j=0;j
我的第一个问题是如何将字符串转换为PictureBox。
然后将PictureBox的每个项目转换为位图,然后打印所有位图现在代码只打印一个条形码

表示您有字符串,并且已将其转换为条形码。最后,我们在条形码中有一个属性,它保存字符串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("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;
    }
我相信,如果
bar1.Encode
实际上有返回类型
Image
,那么
PrintButton\u点击
方法中的这行代码:

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\u 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();
   }
}
private void PrintButton\u单击(对象发送者,事件参数e)
{
for(int j=0;j

你想要这样。。正当请看这是这个字符串“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的每个元素传递到位图。如何将每个元素的宽度和高度传递给位图构造函数?很抱歉,我不知道“创建条形码…”是什么意思。我可以告诉你如何从字符串列表中创建具有相同图像的图像列表或图像框列表-字符串列表->图像列表-字符串列表->图片框列表是的,我想要这就是你所说的你是说你想要字符串列表中的图像列表对吗??然后你也可以使用上面的代码。。为列表中的每个字符串调用上述函数或更改函数本身,该函数接受字符串列表并返回图像列表。但我希望图像的格式为条形码。为此,我使用的是Encode方法Inner
PrintButton\u Click
方法,您使用了
j
作为循环计数器变量,但是在循环中使用了
i
所有代码,而不是
j
。输入错误或…?抱歉编辑了我的代码@HAR07第一个将给出索引超出范围的异常,因为我也尝试过它,第二个将工作良好,如果我知道PictureBox的数量,但在我的情况下,我不知道我需要多少PictureBox,所以要使其动态意味着,您需要替换部分代码(在我答案的第一个代码块中显示)使用第二个代码块。无需知道PictureBox的数量。我已经使用了您的代码,但现在我必须将其打印到一页,为此我必须将其转换为位图,并传递每个PictureBox的宽度和高度。我可以将此值设置为PictureBox,还是只需制作一个图像列表?当我单击P时,它不会打印“打印”按钮它给我空页面图标你必须为特定或所有你想要的字符串调用此方法。它将一次调用返回图像,然后对该图像执行任何你想要的操作。由你决定,你想显示销毁保存或任何你想要的内容。是的,它在PictureBox中显示图像,但我如何打印此图像?”在页面上?:/真的很困惑。我已经为你创建了示例,但这是窗口窗体。我的意思是尝试给出字符串并单击以生成它。它的列表基于你想要的内容。我将上传到我的谷歌文档中,并与你共享链接。。