Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows Forms
is there any method for changing pictures with animation instead of them just popping up?
Posted 26-Mar-13 11:37am
Comments
Kenneth Haugland at 26-Mar-13 16:39pm
   
You mean by transperancy or something different?
Code-genious at 26-Mar-13 16:56pm
   
rotation
Sergey Alexandrovich Kryukov at 26-Mar-13 17:23pm
   
???
 
Anyway, please see my answer. It has 100% of what you need (it's a pretty popular question).
—SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

What popping? No.
 
You need to render the graphics is some control by overriding the method System.Windows.Control.OnPaint or by handling the event System.Windows.Control.Paint. Every time you invalidate the control or a part of it, this method or your handler method will be called. It will show the fixed picture.
 
But your rendering may depend on some data. You can keep some parameters of graphics in the field(s) of you control class (including Form) and change is according to some scenario. Write this scenario in a separate thread with delay using System.Threading.Thread.Sleep. In this thread, modify this data step by step, which will make your frames. Don't forget about thread synchronization (use lock statements where needed). Now, how to re-render the steps? Use System.Windows.Control.Invalidate. You can improve performance if you use the Invalidate methods invalidating only the part of the scene (a Rectangle or Region).
 
No, you can only call Invalidate in the UI thread. So, what to do? 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[^].
 
And finally, to avoid flicker, you might need to use double buffering.
 
See also my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].
 
By the way, by all means, avoid the temptation to use PictureBox by all means. It is possible, but not helpful at all. Please see my answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100