Click here to Skip to main content
16,007,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to change image alternatively in picturebox control in vb.net?
Posted

Apparently, by assigning alternative values to its property Image, http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx[^].

This is simple enough to do it in this way.

In case you will need something even a bit more complex than that, consider not using PictureBox at all. Instead, see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^].

See also:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

[EDIT]

If you need periodic flipping of the image, I should warn you: it will irritate most of the users very much, so it's the best to avoid it.

Don't use System.Windows.Forms.Timer: it is too bad, won't behave periodically. Other two timers types are good enough enough, but a separate thread with periodic flip and Sleep is much easier and more reliable. In both timer and thread cases, your handler will be called from some non-UI thread, and you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
v3
Use a timer to change picture alternatively in picturebox.
Write inside a timer:
t = t + 1
If t = 2 Then

PictureBox1.Image = Image.FromFile("Image Path")
End If

If t = 5 Then
PictureBox1.Image = Image.FromFile("Image Path")

End If

If t = 8 Then
t = 0
End If
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Feb-12 5:03am    
1) timer is bad, thread is much better, 2) code is very bad, won't even work, why 4, 8? the calculation is simple: bool a; a = !a;, repeatedly creates image from file, waste a lot of performance; 3) will case cross-thread exception, needs Invoke unless it it Forms.Timer which won't show periodic behavior due to prohibitively bad accuracy. Not acceptable as the answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900