Displaying an array of images in picturebox?
27,749
Solution 1
Edit-1 : This answer has a scope limited to Win-Forms C#. You need certain assemblies added in your application before using this code.
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]);
}
Solution 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
Solution 3
Use picturebox[index].Image = Image.FromFile(list[index]);
Author by
Admin
Updated on October 08, 2020Comments
-
Admin over 3 years
I'm very new to visual C# I want to display an array of images in a picture box
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]); }
-
Mohammed Abrar Ahmed almost 8 yearsI am getting errors saying
does not contain defination for AutoScroll , Directory, PictureBox, Controls, Points, Size, Image
please tell me which directive should i add. -
Sami almost 8 yearsYou should be using winf-forms to use this code. Please see my edit at top
-
Shubhamoy over 7 yearsTry to explain your answer for getting proper attention. SO doesn't believes in code snippet as answers.