Solved

display arraylist in picture box

Posted on 2008-10-09
34
1,759 Views
Last Modified: 2013-12-17
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
Comment
Question by:HawaiiDragon
  • 16
  • 13
  • 5
34 Comments
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22680307
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22680358
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
 

Author Comment

by:HawaiiDragon
ID: 22680610
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22680647
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
 

Author Comment

by:HawaiiDragon
ID: 22680675
yes thats right.
0
 

Author Comment

by:HawaiiDragon
ID: 22680757
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22680770
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
 

Author Comment

by:HawaiiDragon
ID: 22680861
 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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22680872
What error does it give you?
And in what method of the form does that code live in?
0
 

Author Comment

by:HawaiiDragon
ID: 22681268
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
 

Author Comment

by:HawaiiDragon
ID: 22681286
do you have a msn or yahoo?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22681372
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
 

Author Comment

by:HawaiiDragon
ID: 22681458
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681480
At first glance, that looks OK.
What do you mean....still nothing.
An Error?
0
 

Author Comment

by:HawaiiDragon
ID: 22681517
No errors just nothing displayed in picture box
0
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681528
Nothing displayed in Picture Box AFTER you click button 1 ...right?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22681534
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22681552
You also have a type O in the image file name of,  myImages[1] = new Bitmap("mage2.jpg");
0
 

Author Comment

by:HawaiiDragon
ID: 22681586
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681627
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681630
"Tourbleshooting...." = that's French for "troubleshooting" don't ya know.
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 50 total points
ID: 22681643
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681684
I am pretty sure that the "new Bitmap("xxxxxxx");" call will bomb if the file cannot be found.
0
 

Author Comment

by:HawaiiDragon
ID: 22681904
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681968
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22681973
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
 

Author Comment

by:HawaiiDragon
ID: 22682020
Im getting 'CurrentImageNum does not exist in the current context <-- error
0
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22682035

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
 

Author Comment

by:HawaiiDragon
ID: 22682076
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
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22682106
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
 
LVL 7

Accepted Solution

by:
joesthebighmoe earned 450 total points
ID: 22682107

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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22682131
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
 

Author Comment

by:HawaiiDragon
ID: 22682191
Thank you for explaining this mess to me!!! Your awesome.
0
 
LVL 7

Expert Comment

by:joesthebighmoe
ID: 22682245
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now