在图片框中显示图像数组?

时间:2021-01-30 00:24:04

I'm very new to visual C# I want to display an array of images in a picture box

我是视觉C#的新手我希望在图片框中显示一系列图像

Here's my code:

这是我的代码:

string[] list = Directory.GetFiles(@"C:\\pictures", "*.jpg");
Image[] images = new Image[5];
for (int index = 0; index < 5; index++)

{
    //HERE IS WHERE IM STUCKED WITH
    picturebox[index] = Image.FromFile(list[index]);
}

5 个解决方案

#1


3  

Edit-1 : This answer has a scope limited to Win-Forms C#. You need certain assemblies added in your application before using this code.

编辑-1:此答案的范围仅限于Win-Forms C#。在使用此代码之前,您需要在应用程序中添加某些程序集。

using System.IO;
using System.Windows.Forms;

Edit ended;

编辑结束;

Original Answer

原始答案

You have to draw all image to one image for displaying them in single picturebox

您必须将所有图像绘制到一个图像,以便在单个图片框中显示它们

That is bit complex you can use mutiple pictureboxes

这有点复杂,你可以使用多个图片框

In following code they are created dynamically according to need:

在以下代码中,它们是根据需要动态创建的:

    // For confirm visibility of all images set 
    this.AutoScroll = true;

    string[] list = Directory.GetFiles(@"C:\pictures", "*.jpg");
    PictureBox[] picturebox= new PictureBox[list.Length];
    int y = 0;
    for (int index = 0; index < picturebox.Length; index++)
    {
        this.Controls.Add(picturebox[index]);
        // Following three lines set the images(picture boxes) locations
        if(index % 3 == 0)
            y = y + 150; // 3 images per rows, first image will be at (20,150)
        picturebox[index].Location=new Point(index * 120 + 20, y);

        picturebox[index ].Size = new Size(100,120);
        picturebox[index].Image = Image.FromFile(list[index]);
    }

#2


2  

The answer provided throws an object reference exception. Otherwise thanks for the example!

提供的答案抛出了一个对象引用异常。否则谢谢你的例子!

for (int index = 0; index < picturebox.Length; index++)
{
     this.Controls.Add(picturebox[index]);
     // Following three lines set the images(picture boxes) locations

should be

应该

for (int index = 0; index < picturebox.Length; index++)
{
    picturebox[index] = new PictureBox();
    this.Controls.Add(picturebox[index]);
    // Following three lines set the images(picture boxes) locations

#3


1  

Use picturebox[index].Image = Image.FromFile(list[index]);

使用picturebox [index] .Image = Image.FromFile(list [index]);

#4


0  

//this code help you to work with picturebox in arraye

//此代码可帮助您在arraye中使用picturebox

public partial class Form_Begin : Form
    {
        PictureBox[] pictureBoxs = new PictureBox[50];
        public Form_Begin()
        {
            InitializeComponent();
            pictureBoxs[0] = pictureBox1;
            pictureBoxs[1] = pictureBox2;
            pictureBoxs[2] = pictureBox3;
            pictureBoxs[3] = pictureBox4;}



            List<PictureBox> pictureBoxes = new List<PictureBox>();
 private void buttonX1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i <2; i++)
                {
                    pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_1;                     // Load Image_1 from Resources on property of picturebox  
                }
                for (int i = 2; i < 4; i++)
                {
                    pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_2;                    // Load Image_12 from Resources on property of picturebox 

                }

#5


0  

 private void picbutton_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        PictureBox[] picture = new PictureBox[5];
        int x = 0;
        int y = 15;
        for (int index = length; index < picture.Length; index++)
        {
                picture[index] = new PictureBox();
                picture[index].Size = new Size(100, 50);
                open.Title = "OPen Image";
                open.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif";
                DialogResult result = open.ShowDialog();
                if (result == DialogResult.OK)
                {
                    picture[index].BackgroundImage = new Bitmap(open.FileName);
                    picture[index].SizeMode = PictureBoxSizeMode.AutoSize;
                    listBox1.Controls.Add(picture[index]);
                    if ((x % 3 == 0) && (index != 0))
                    {
                        y = y + 150; // 3 images per rows, first image will be at (20,150)
                        x = 0;
                    }
                    picture[index].Location = new Point(x * 210 + 20, y);
                    picture[index].Size = new Size(200, 150);
                    x++;
            }
        }
    }

#1


3  

Edit-1 : This answer has a scope limited to Win-Forms C#. You need certain assemblies added in your application before using this code.

编辑-1:此答案的范围仅限于Win-Forms C#。在使用此代码之前,您需要在应用程序中添加某些程序集。

using System.IO;
using System.Windows.Forms;

Edit ended;

编辑结束;

Original Answer

原始答案

You have to draw all image to one image for displaying them in single picturebox

您必须将所有图像绘制到一个图像,以便在单个图片框中显示它们

That is bit complex you can use mutiple pictureboxes

这有点复杂,你可以使用多个图片框

In following code they are created dynamically according to need:

在以下代码中,它们是根据需要动态创建的:

    // For confirm visibility of all images set 
    this.AutoScroll = true;

    string[] list = Directory.GetFiles(@"C:\pictures", "*.jpg");
    PictureBox[] picturebox= new PictureBox[list.Length];
    int y = 0;
    for (int index = 0; index < picturebox.Length; index++)
    {
        this.Controls.Add(picturebox[index]);
        // Following three lines set the images(picture boxes) locations
        if(index % 3 == 0)
            y = y + 150; // 3 images per rows, first image will be at (20,150)
        picturebox[index].Location=new Point(index * 120 + 20, y);

        picturebox[index ].Size = new Size(100,120);
        picturebox[index].Image = Image.FromFile(list[index]);
    }

#2


2  

The answer provided throws an object reference exception. Otherwise thanks for the example!

提供的答案抛出了一个对象引用异常。否则谢谢你的例子!

for (int index = 0; index < picturebox.Length; index++)
{
     this.Controls.Add(picturebox[index]);
     // Following three lines set the images(picture boxes) locations

should be

应该

for (int index = 0; index < picturebox.Length; index++)
{
    picturebox[index] = new PictureBox();
    this.Controls.Add(picturebox[index]);
    // Following three lines set the images(picture boxes) locations

#3


1  

Use picturebox[index].Image = Image.FromFile(list[index]);

使用picturebox [index] .Image = Image.FromFile(list [index]);

#4


0  

//this code help you to work with picturebox in arraye

//此代码可帮助您在arraye中使用picturebox

public partial class Form_Begin : Form
    {
        PictureBox[] pictureBoxs = new PictureBox[50];
        public Form_Begin()
        {
            InitializeComponent();
            pictureBoxs[0] = pictureBox1;
            pictureBoxs[1] = pictureBox2;
            pictureBoxs[2] = pictureBox3;
            pictureBoxs[3] = pictureBox4;}



            List<PictureBox> pictureBoxes = new List<PictureBox>();
 private void buttonX1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i <2; i++)
                {
                    pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_1;                     // Load Image_1 from Resources on property of picturebox  
                }
                for (int i = 2; i < 4; i++)
                {
                    pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_2;                    // Load Image_12 from Resources on property of picturebox 

                }

#5


0  

 private void picbutton_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        PictureBox[] picture = new PictureBox[5];
        int x = 0;
        int y = 15;
        for (int index = length; index < picture.Length; index++)
        {
                picture[index] = new PictureBox();
                picture[index].Size = new Size(100, 50);
                open.Title = "OPen Image";
                open.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif";
                DialogResult result = open.ShowDialog();
                if (result == DialogResult.OK)
                {
                    picture[index].BackgroundImage = new Bitmap(open.FileName);
                    picture[index].SizeMode = PictureBoxSizeMode.AutoSize;
                    listBox1.Controls.Add(picture[index]);
                    if ((x % 3 == 0) && (index != 0))
                    {
                        y = y + 150; // 3 images per rows, first image will be at (20,150)
                        x = 0;
                    }
                    picture[index].Location = new Point(x * 210 + 20, y);
                    picture[index].Size = new Size(200, 150);
                    x++;
            }
        }
    }