Reputation: 639
Few days ago I've started working on my own photo viewer/editor. I've implemented method "OnKeyDown" which changes photos depending of which arrow key is pressed:
case Keys.Left:
case Keys.Down:
ic.getImage().Dispose();
--fileIndex;
if (fileIndex < 0)
fileIndex = (fileCount - 1);
ic.setImage(Image.FromFile(fileNames[fileIndex]), ref pictureBox1);
break;
case Keys.Right:
case Keys.Up:
ic.getImage().Dispose();
++fileIndex;
fileIndex %= fileCount;
ic.setImage(Image.FromFile(fileNames[fileIndex]), ref pictureBox1);
break;
This works perfectly. Next thing I wanted to do is to program the slideshow, so I used System.Timers.Timer to trigger the event goRight() which executes the same code as if right arrow key is pressed:
public void goRight(object source, ElapsedEventArgs e)
{
ic.getImage().Dispose();
++fileIndex;
fileIndex %= fileCount;
ic.setImage(Image.FromFile(fileNames[fileIndex]), ref pictureBox1);
}
When using slideshow app just breaks. I can't figure out why, because the code is exactly the same. If I comment out the ic.getImage().Dispose() slideshow works but the amount of data in RAM goes higher and higher with each next photo.
Am I doing something wrong? Thank you for your time, I appreciate it!
Uh, I almost forgot. Is there a way to make indexes go backwards without if statement (in key down and key left events) (like operator % for forwards). I did try this:
++fileIndex;
fileIndex %= fileCount;
ic.setImage(Image.FromFile(fileNames[(fileCount -1) - fileIndex]), ref pictureBox1);
And it works well if I go in just one direction, but if I change direction it skips few indexes. Any thoughts about that?
Best regards. Z
Upvotes: 0
Views: 86
Reputation: 3612
For the indexes you could do something like:
fileIndex = (fileIndex == 0) ? fileCount -1 : fileIndex - 1; // decrementing
fileIndex = (fileIndex == fileCount - 1) ? 0 : fileIndex + 1; // incrementing
and as cHao says, use System.Windows.Forms.Timer instead.
Upvotes: 1