Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1836
  • Last Modified:

display arraylist in picture box

Hi guys Im having some problems. Im trying to make a slide show for work and I have 5 immages. I have it in a windows form with a priviouse,1,2,3,4,5,next buttons. Its in a windows form.
I have never designed an array before. the images are held in a folder called images. Any assistance would be greatly appriciated. The button clicks should acces the array with the specific pictures.
0
HawaiiDragon
Asked:
HawaiiDragon
  • 16
  • 13
  • 5
2 Solutions
 
joesthebighmoeCommented:
Usually, on a C# Forms application that is "flipping" through images we would use the ImageList object. It is in the Components section in the toolbox in the IDE.
0
 
joesthebighmoeCommented:
You can set the images at design time.
I was assuming this does not have to be dynamic.
Drag and Drop an ImageList object on your form.
In the properties, click on (...) button and add all your images.

In code you can reference the images by their index.
For example, when the user clicks on the first button your code might look like this...
myPictureBox.Image = myImageList.Images[0];
When the user clicks on the second button your code might look like this...
myPictureBox.Image = myImageList.Images[1];
0
 
HawaiiDragonAuthor Commented:
how would i do this?
Array [5] my array = new array();
Unfortnatly I do have to do the aray and load the pics dynamicaly.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
joesthebighmoeCommented:
How would you do what exactly?
Are you telling me there are rules to how you have to implement your solution?
Sounds like a homework assignment.

You have pictures in an images directory.
You have buttons that need to show a corresponding image.
So, instead of just pointing an ImageList as those images and then getting them out as needed you instead HAVE to write a good deal more code to load the images, add them to an array, cast them as image objects, and then display them?

Is that right?


0
 
HawaiiDragonAuthor Commented:
yes thats right.
0
 
HawaiiDragonAuthor Commented:
In school we learned nothing about dynamicaly creating arrays. It sucks, so my boss has givin me some learn time asignments to try and figure out what to do so that I will be a better programmer. Im just stuck and need some help
0
 
joesthebighmoeCommented:
And they say the US is a free country.....sheesh!   ;-)

So do we know there are only 6 pictures?
Also, what kind of pictures are they? bmp, jpb, ico, etc?
Are they all the same?

The easiest way to load an image up into memory is using the BitMap object.
Below is the code to load up 6 images and add them to an array.
Bitmap[] myImages = new Bitmap[5];
      myImages[0] = new Bitmap(@"C:\images\Image1.jpg");
      myImages[1] = new Bitmap(@"C:\images\Image2.jpg");
      myImages[2] = new Bitmap(@"C:\images\Image3.jpg");
      myImages[3] = new Bitmap(@"C:\images\Image4.jpg");
      myImages[4] = new Bitmap(@"C:\images\Image5.jpg");
      myImages[5] = new Bitmap(@"C:\images\Image6.jpg");

Open in new window

0
 
HawaiiDragonAuthor Commented:
 public MySlideShow()
        {
            InitializeComponent();
        }

        Bitmap[] myImages = new Bitmap[5];
        myImages [0] = new Bitmap(@"\images\image1.jpg");
        myImages [1] = new Bitmap(@"\images\image2.jpg");
        myImages [2] = new Bitmap(@"\images\image3.jpg");
        myImages [3] = new Bitmap(@"\images\image4.jpg");
        myImages [4] = new Bitmap(@"\images\image5.jpg");
       

this is what ive done but its giving me errors.. the images is tne name of the image dir in the solution.
0
 
joesthebighmoeCommented:
What error does it give you?
And in what method of the form does that code live in?
0
 
HawaiiDragonAuthor Commented:
well.... 20 errors to start but first one is
Array size cannot be specified in a variable delclartion (try initializing with a  "new" expression
Invaled token '=' in class, struct, or interface member declaration
0
 
HawaiiDragonAuthor Commented:
do you have a msn or yahoo?
0
 
Fernando SotoCommented:
Hi HawaiiDragon;

It looks like from your previous post that you are trying to initialize the array outside of any function, at class level, if so that will give you errors.

See the code snippet. the way I have the images being load they must be in the same directory as the executable. If that is not the case then give full path in the statement as shown here:

 myImages [0] = new Bitmap(@"\images\image1.jpg");

Fernando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication56
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        Bitmap[] myImages;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            myImages = new Bitmap[5];
            myImages[0] = new Bitmap("Image1.jpg");
            myImages[1] = new Bitmap("Image2.jpg");
            myImages[2] = new Bitmap("Image3.jpg");
            myImages[3] = new Bitmap("Image4.jpg");
            myImages[4] = new Bitmap("Image5.jpg");
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[0];
        }
    }
}

Open in new window

0
 
HawaiiDragonAuthor Commented:
This is what I have done and still nothing. Any ideas?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
 
 
 
 
 
 
namespace Sideshow
{
    public partial class MySlideShow : Form
    {
        public MySlideShow()
        {
            InitializeComponent();
        }
 
        Bitmap[] myImages = new Bitmap[5];
 
        private void Form1_Load(object sender, EventArgs e)
        {
            myImages[0] = new Bitmap("image1.jpg");
            myImages[1] = new Bitmap("mage2.jpg");
            myImages[2] = new Bitmap("image3.jpg");
            myImages[3] = new Bitmap("image4.jpg");
            myImages[4] = new Bitmap("image5.jpg");
        }
 
        private void btn1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[0];
        }
 
    }
}

Open in new window

0
 
joesthebighmoeCommented:
At first glance, that looks OK.
What do you mean....still nothing.
An Error?
0
 
HawaiiDragonAuthor Commented:
No errors just nothing displayed in picture box
0
 
joesthebighmoeCommented:
Nothing displayed in Picture Box AFTER you click button 1 ...right?
0
 
Fernando SotoCommented:
Do you have 5 jpg files in the same directory as the executable named image1.jpg, image2.jpg, ..., image5.jpg,?

After the form loads did you click on the button?
0
 
Fernando SotoCommented:
You also have a type O in the image file name of,  myImages[1] = new Bitmap("mage2.jpg");
0
 
HawaiiDragonAuthor Commented:
folder name: images - Image1.jpg, image2.jpg, image3.jpg, image4.jpg, image5.jpg
i tried to move it and it says i'destination folder is same as source folder'  

-----After the form loads did you click on the button?

yes and nothing shows up in the form.
0
 
joesthebighmoeCommented:
Tourbleshooting....
Do me a favor...
Move this line of code
               pictureBox1.Image = myImages[0];
Directly after this line...
myImages[4] = new Bitmap("image5.jpg");

Now try it
0
 
joesthebighmoeCommented:
"Tourbleshooting...." = that's French for "troubleshooting" don't ya know.
0
 
Fernando SotoCommented:
The image files need to be in the as directory as the executable otherwise you will need to give the full path to each file in the code, if this is not done the images will not get loaded. If you are coding in Visual Studio .Net the executable file, if you are working in the Debug configuration, will be two directory levels down, bin/Debug.
0
 
joesthebighmoeCommented:
I am pretty sure that the "new Bitmap("xxxxxxx");" call will bomb if the file cannot be found.
0
 
HawaiiDragonAuthor Commented:
Well a couple of things were haywire.

A. When I had the Form1_Load when i changed the name of the form it did not change the access to it at all. That was problem 1 then I needed to put
  private void Form1_Load(object sender, EventArgs e)
        {
            myImages[0] = new Bitmap(@"..\..\images\Image1.jpg");
            myImages[1] = new Bitmap(@"..\..\images\Image2.jpg");
            myImages[2] = new Bitmap(@"..\..\images\Image3.jpg");
            myImages[3] = new Bitmap(@"..\..\images\Image4.jpg");
            myImages[4] = new Bitmap(@"..\..\images\Image5.jpg");
        }

you were right about the bin file system. So thank you.
Now how do I make the priviouse and next buttons work?

   private void btnPrevious_Click(object sender, EventArgs e)
        {
                 
        }

        private void btnNext_Click(object sender, EventArgs e)
        {

        }
0
 
joesthebighmoeCommented:
Something needs to track the current image you are looking at.
For simplicity's sake, we can create a member variable called _CurrentImageNum;
So add this line right after the "Bitmap[] myImages;" line.
private int CurrentImageNum = 0;
This will set our current image number to 0 - our first image.

Now, inside the button click methods we have to change the current number of the image we are wanting to view...

 private void btnNext_Click(object sender, EventArgs e)
{
    //If the user has not reached the last image, then move to the next image.
   if(CurrentImageNum < 4)
   {
    CurrentImageNum +=1; //that will add one to the variable.
    }
    pictureBox1.Image = myImages[CurrentImageNum ];
}

 private void btnPrevious_Click(object sender, EventArgs e)
{
    //If the user has not reached the last image, then move to the next image.
   if(CurrentImageNum > 0)
   {
    CurrentImageNum -=1; //that will subtract one from the variable.
    }
    pictureBox1.Image = myImages[CurrentImageNum ];
}

0
 
joesthebighmoeCommented:
Oops in the btnPrevious_Click method, my comment should read...
 //If the user has not reached the first image, then move to the previous image.
0
 
HawaiiDragonAuthor Commented:
Im getting 'CurrentImageNum does not exist in the current context <-- error
0
 
joesthebighmoeCommented:

This variable needs to be accessed anywhere in the form.
So we have to declare in outside of any method.
Make sure it is declared right after
Bitmap[] myImages = new Bitmap[5];
0
 
HawaiiDragonAuthor Commented:
Im not understanding I dont have a bitmap 5.
Pluss there is no variable for CurrentImageNum its not in Intellisence
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
 
 
 
 
 
 
namespace Sideshow
{
    public partial class MySlideShow : Form
    {
        public MySlideShow()
        {
            InitializeComponent();
        }
 
        Bitmap[] myImages = new Bitmap[5];
 
        private void Form1_Load(object sender, EventArgs e)
        {
            myImages[0] = new Bitmap(@"..\..\images\Image1.jpg");
            myImages[1] = new Bitmap(@"..\..\images\Image2.jpg");
            myImages[2] = new Bitmap(@"..\..\images\Image3.jpg");
            myImages[3] = new Bitmap(@"..\..\images\Image4.jpg");
            myImages[4] = new Bitmap(@"..\..\images\Image5.jpg");
        }
 
        private void btn1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[0];
            pictureBox1.Refresh();
        }
 
        private void btn2_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[1];
            pictureBox1.Refresh();
        }
 
        private void btn3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[2];
            pictureBox1.Refresh();
        }
 
        private void btn4_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[3];
            pictureBox1.Refresh();
        }
 
        private void btn5_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = myImages[4];
            pictureBox1.Refresh();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void btnNext_Click(object sender, EventArgs e)
       
        {
            if (Image.ReferenceEquals < 4)
            {
                Image.ReferenceEquals += 1;
            }
            pictureBox1.Image[0];
            pictureBox1.Refresh();
        }
 
            //{
        //    if (CurrentImageNum < 4)
        //    {
        //        CurrentImageNum += 1;
        //    }
        //    pictureBox1.Image = myImages[CurrentImageNum];
      //  }
 
 
    }
}

Open in new window

0
 
joesthebighmoeCommented:
You have a member variable called myImages.
You need to add the line I am telling you about right after it.

 public MySlideShow()
 {
 InitializeComponent();
}
 
        Bitmap[] myImages = new Bitmap[5];  <--LOOK HERE  <--LOOK HERE  <--LOOK HERE
        private int CurrentImageNum = 0;  <--ADD THIS LINE  <--ADD THIS LINE  <--ADD THIS LINE  
       
0
 
joesthebighmoeCommented:

public MySlideShow()
 {
 InitializeComponent();
}
 
        Bitmap[] myImages = new Bitmap[5];  <--LOOK HERE  <--LOOK HERE  <--LOOK HERE 
        private int CurrentImageNum = 0;  <--ADD THIS LINE  <--ADD THIS LINE       

Open in new window

0
 
Fernando SotoCommented:
Try this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication56
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        Bitmap[] myImages;
        int currentImage;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            myImages = new Bitmap[5];
            myImages[0] = new Bitmap(@"..\..\images\Image1.jpg");
            myImages[1] = new Bitmap(@"..\..\images\Image2.jpg");
            myImages[2] = new Bitmap(@"..\..\images\Image3.jpg");
            myImages[3] = new Bitmap(@"..\..\images\Image4.jpg");
            myImages[4] = new Bitmap(@"..\..\images\Image5.jpg");
            pictureBox1.Image = myImages[0];
            currentImage = 0;
        }
 
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentImage >= 1)
            {
                currentImage--;
                pictureBox1.Image = myImages[currentImage];
            }
            else
            {
                MessageBox.Show("You are at the first image already.");
            }
        }
 
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentImage <= 3)
            {
                currentImage++;
                pictureBox1.Image = myImages[currentImage];
            }
            else
            {
                MessageBox.Show("You are at the last image already.");
            }
        }
 
 
    }
}

Open in new window

0
 
HawaiiDragonAuthor Commented:
Thank you for explaining this mess to me!!! Your awesome.
0
 
joesthebighmoeCommented:
Really quick....to make sure the right lessons have been learned.

1. To declare an array of some type that is not dynamic (we know the number of types in the array at the beginning) you do this...
Type[] VariableName = new Type[number of types];
So in your case...
Bitmap[] myImages = new Bitmap[5];  

2. Variables can only be accessed within the scope that they were declared. If you declare a variable inside a method, it can only be accessed inside that method. If you declare a private variable inside a class (like your form) but not in a method, then only the methods in that class can see the variable.  

Good luck
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 16
  • 13
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now