image loop in vb.net
-
Hello all I want to loop images in my application when form loads it loads all the images and displays last image in picture box but i want to display all the images in picture box one by one i mean it starts from image 1 to image 15 and when it's image 15 it should start from image 1 again. this is what i have done
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call LoadImages()
End SubPrivate Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick For Me.I = 0 To 15 PictureBox1.Image = (MyImage(I)) Next I End Sub Private Sub LoadImages() MyImage(0) = My.Resources.G1 MyImage(1) = My.Resources.G10 MyImage(2) = My.Resources.G11 MyImage(3) = My.Resources.G12 MyImage(4) = My.Resources.G13 MyImage(5) = My.Resources.G14 MyImage(6) = My.Resources.G15 MyImage(7) = My.Resources.G16 MyImage(8) = My.Resources.G2 MyImage(9) = My.Resources.G3 MyImage(10) = My.Resources.G4 MyImage(11) = My.Resources.G5 MyImage(12) = My.Resources.G6 MyImage(13) = My.Resources.G7 MyImage(14) = My.Resources.G8 MyImage(15) = My.Resources.G9 End Sub
it should keep displaying images until user close this application but image should be in right order according to myimage array please help :confused:
-
Look at LoadImages. Assuming that you want the sequence to go in the order G1, G2, G3, G4, etc., you're setting the array in the wrong order. You load G10 before G2, and so on
Between the idea And the reality Between the motion And the act Falls the Shadow
hello thanks for your rep. no the order in array is right order so all the images should display as per array order what i what to do is when i start app it should display images until i close the app. i mean first image in array and then 2nd and 3rd up to 16th and then 1st image again but now it's load all the images and display only 16th image in to the picture box i want it keep displaying images from first image in array to last image in array and then go back to first image and so on waiting for your kind help
-
Hello Thanks for your answer you mean some thing like ths
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
For Me.I = 0 To 15
PictureBox1.Image = (MyImage(I))
Next I
End Subif yes then i have tried but still the same i know there is very small mistake but i am not getting there :confused:
Hi, Every time the timer event fires you want to display a different image, correct? Within your handler you have written a loop that loads all of the images every time it is called, hence you only ever see the last one, MyImage(15). The code should select one index, i.e. one of the values 0 to 15, and then load the image referenced by the index into the picture box. Alan.
-
hello thanks for your rep. no the order in array is right order so all the images should display as per array order what i what to do is when i start app it should display images until i close the app. i mean first image in array and then 2nd and 3rd up to 16th and then 1st image again but now it's load all the images and display only 16th image in to the picture box i want it keep displaying images from first image in array to last image in array and then go back to first image and so on waiting for your kind help
Something like this? :)
Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Private Shared nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod 16 End Sub
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
Something like this? :)
Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Private Shared nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod 16 End Sub
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
Unless I have misread your code, it would not work! Every time the tick event fires you are setting
nextImage
to 0, before doing anything else, therefore the last line of your snippet is useless. The OP should use something like this:Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick PictureBox1.Image = MyImage(Me.I) Me.I = (Me.I + 1) Mod 16 End Sub
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Unless I have misread your code, it would not work! Every time the tick event fires you are setting
nextImage
to 0, before doing anything else, therefore the last line of your snippet is useless. The OP should use something like this:Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick PictureBox1.Image = MyImage(Me.I) Me.I = (Me.I + 1) Mod 16 End Sub
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
nextImage is static (i.e. Shared in VB) so it is set to 0 only on first call to Timer1_Tick. It retains its value between successive calls which is what I think the OP requires. The problem with Me.I is it could be changed outside of the handler by some unrelated code. When the handler is called you have no idea what the value of Me.I is so it's wide open to index out of bounds exception.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
nextImage is static (i.e. Shared in VB) so it is set to 0 only on first call to Timer1_Tick. It retains its value between successive calls which is what I think the OP requires. The problem with Me.I is it could be changed outside of the handler by some unrelated code. When the handler is called you have no idea what the value of Me.I is so it's wide open to index out of bounds exception.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
OK. Sorry I missed that. :-O
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
OK. Sorry I missed that. :-O
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Easily done - often guilty of it myself. :) When I thought about the bounds it struck me that this is better.
Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Private Shared nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod MyImage.Length End Sub
Then you don't have to do anything if more images are added or some removed.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
-
Easily done - often guilty of it myself. :) When I thought about the bounds it struck me that this is better.
Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Private Shared nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod MyImage.Length End Sub
Then you don't have to do anything if more images are added or some removed.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
hello thanks for your rep. yes it's works fine this is what i changed
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'PictureBox1.Image = (MyImage(I)) Static nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod MyImage.Length End Sub
it's working as i was trying to thanks a lot sir have a nice day :-D
-
Easily done - often guilty of it myself. :) When I thought about the bounds it struck me that this is better.
Private Sub Timer1\_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick Private Shared nextImage As Integer = 0 PictureBox1.Image = MyImage(nextImage) nextImage = (nextImage + 1) Mod MyImage.Length End Sub
Then you don't have to do anything if more images are added or some removed.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis
riced wrote:
Then you don't have to do anything if more images are added or some removed.
Good point. I really should practice VB.Net more, if I want to play over here. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”