I have a problem cycling through an image list by pressing the next button and displaying the selected image in picturebox. What I want to do in my code is for user to click btnNext and to check if image value in picture box is less than what’s in the image list.
If it is then select that next image in the listbox (containing imagelist) and display it in picturebox. I’m not sure how to do all that though. Here is the code. Just to be clear the error is in 2nd if statement of btnNext_Click. Error says "Operator '>' cannot be applied to operands of type 'int' and 'Image'" GUI at runtime
string _big_fileName;
int _counter = 0;
public Form1()
{
InitializeComponent();
}
//Displays larger instance of selected image in picture box.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
//FOR i is less than the first image.
for (int i = 0; i < listView1.SelectedItems.Count;i++)
{
//GET filename from listview and store in index.
_big_fileName = listView1.SelectedItems[i].Text;
//Create larger instance of image.
pictureBox1.Image = Image.FromFile(_big_fileName);
//Fill panel to the width and height of picture box.
panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width,
pictureBox1.Image.Height);
}
}
private void mnuOpen_Click(object sender, EventArgs e)
{
loadImageList();
}
private void btnNext_Click(object sender, EventArgs e)
{
if(pictureBox1.Image != null)
{
//IF Image is less than Image list size.
if (pictureBox1.Image < imageList1.Images.Count)
{
//ACCESS Imagelist size against image value
_big_fileName = listView1.SelectedItems[_counter].Text;
//INCREMENT Image list current position.
_counter++;
//ASSIGN current position to image.
pictureBox1.Image = Image.FromFile(_big_fileName);
//DISPLAY and enlarge image.
panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width,
pictureBox1.Image.Height);
}
}
else
{
MessageBox.Show("No image.");
}
}
private void loadImageList()
{
imageList1.Images.Clear();
listView1.Clear();
oFD1.InitialDirectory = "C:\\";
oFD1.Title = "Open an Image File";
oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif|PNGS|*.png|BMPS|*.bmp";
//Open Dialog Box.
var oldResults = oFD1.ShowDialog();
if (oldResults == DialogResult.Cancel)
{
return;
}
try
{
//GET amount of filenames.
int num_of_files = oFD1.FileNames.Length;
//Store filenames in string array.
string[] arryFilePaths = new string[num_of_files];
//FOREACH filename in the file.
foreach (string single_file in oFD1.FileNames)
{
//ACCESS array using _counter to find file.
arryFilePaths[_counter] = single_file;
//CREATE image in memory and add image to image list.
imageList1.Images.Add(Image.FromFile(single_file));
_counter++;
}
//BIND image list to listview.
listView1.LargeImageList = imageList1;
for (int i = 0; i < _counter; i++)
{
//DISPLAY filename and image from image index param.
listView1.Items.Add(arryFilePaths[i], i);
}
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex.Message);
}
}
Edit: Answer mostly changed based on your comment. You will need to create a new member variable to hold the current image index and make sure that is used to access the ImageList
Create a member variable to hold the current image index:
int _imageIndex;
Add this line to your mnuOpen_Click
function after the call to loadImageList();
_imageIndex = 0;
In the function btnNext_Click
make the changes:
if (pictureBox1.Image < imageList1.Images.Count)
to
if (_imageIndex < imageList1.Images.Count)
and
_big_fileName = listView1.SelectedItems[_counter].Text;
to
_big_fileName = listView1.SelectedItems[_imageIndex].Text;
and add the line
_imageIndex++;
Reworking of the btnNext_Click
event handler
private void btnNext_Click(object sender, EventArgs e)
{
//IF Image is less than Image list size.
if (_imageIndex < imageList1.Images.Count)
{
listView1.Items[_imageIndex].Selected = true;
_imageIndex++;
}
}