If I click one by one click leave the arrow key then a click and leave again and so on the images are change good.
But if I keep pressing the right arrow key nonstop the images dosent change only when I leave the right arrowkey where it stopped so the image in this trackBar value loaded.
This is my scroll event:
private void trackBar1_Scroll(object sender, EventArgs e)
{
currentFrameIndex = trackBar1.Value;
textBox1.Text = "Frame Number : " + trackBar1.Value;
wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex));
LoadPictureAt(trackBar1.Value, sender);
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button8.Enabled = false;
SaveFormPicutreBoxToBitMapIncludingDrawings();
return;
}
I have no other events of the trackBar so far.
This is the LoadPictureAt function:
private bool LoadPictureAt(int nIndex, object c)
{
bool bRet = false;
if (nIndex >= 0 && nIndex < fi.Length)
{
if (c.Equals(trackBar1))
pictureBox1.Load(fi[nIndex].FullName);
bRet = true;
}
return bRet;
}
I SOLVED IT :
I added a new function before my scroll event:
private void setpicture(int indx)
{
if (fi == null)
{
}
else
{
if (indx >= 0 && indx <= trackBar1.Maximum && fi.Length > indx)
{
try
{
label19.ForeColor = Color.Red;
fileToolStripMenuItem.Enabled = true;
label19.Visible = false;
label20.Visible = false;
label14.Visible = true;
label15.Visible = true;
label8.Visible = true;
label9.Visible = true;
trackBar1.Enabled = true;
using (FileStream fs = new FileStream(fi[indx].FullName, FileMode.Open))
{
this.label8.Visible = true;
this.label9.Visible = true;
this.label9.Text = fi[indx].Name;
Image img = null;
Bitmap bmp = null;
Image imgOLd = null;
try
{
img = Image.FromStream(fs);
bmp = new Bitmap(img);
imgOLd = this.pictureBox1.Image;
this.pictureBox1.Image = bmp;
if (imgOLd != null)
imgOLd.Dispose();
img.Dispose();
img = null;
}
catch
{
if (img != null)
img.Dispose();
if (bmp != null)
bmp.Dispose();
if (imgOLd != null)
imgOLd.Dispose();
}
}
}
catch
{
}
}
else
{
Image imgOLd = this.pictureBox1.Image;
if (imgOLd != null)
{
imgOLd.Dispose();
imgOLd = null;
}
Application.DoEvents();
}
}
}
And changed the scroll event to:
private void trackBar1_Scroll(object sender, EventArgs e)
{
currentFrameIndex = trackBar1.Value;
textBox1.Text = "Frame Number : " + trackBar1.Value;
wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex));
trackBar1.Minimum = 0;
trackBar1.Maximum = fi.Length - 1;
setpicture(trackBar1.Value);
pictureBox1.Refresh();
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button8.Enabled = false;
SaveFormPicutreBoxToBitMapIncludingDrawings();
return;
}
There’s no such thing as “non-stop” when you hold down a cursor key or slam it repeatedly as fast as you can. The KeyDown event for it is generated at a glacial pace to modern cpus. You can’t do much better than about 20 hits per second, 50 milliseconds between each hit. A modern core can easily execute a hundred million instructions between keystrokes.
What matters however is what you do with that enormous resource. Unfortunately you don’t do much, you are hitting this disk in the Scroll event. You call Image.Load() and, what looks like, Image.Save(). Those are methods that can only execute as fast as the disk rotates, the cpu isn’t doing anything useful while it waits for the disk to do its job. A hundred million cpu cycles wasted waiting for a mechanical event to happen.
It actually gets worse from there. The next thing that happens is that Winforms needs to paint the window with the image retrieved from the disk. Painting is a low priority operation, it is only done when nothing more important needs to be done.
Problem is, there is something more important that needs to be done. Waiting for the disk to retrieve/save the image took so long that the next thing that the Winforms message loop encounters is something more important than painting. An input event, the next keystroke.
Which gets properly executed, your picture box actually gets its Image assigned. You just can’t see it because it didn’t get painted to the screen. Stop hitting the cursor key and bam, it now has time and paints.
This is all very much by design. You can force a repaint by calling the picture box’ Update() method, the user will now definitely see the image. But that doesn’t actually work very well either, your program starts to lag. The keystrokes are buffered in the message queue and your program will keep updating the image long after you released the key.
Pick your poison.