Click here to Skip to main content
12,997,210 members (76,972 online)
Rate this:
Please Sign up or sign in to vote.
See more:
is there any method for changing pictures with animation instead of them just popping up?
Posted 26-Mar-13 10:37am
Kenneth Haugland 26-Mar-13 16:39pm
You mean by transperancy or something different?
Code-genious 26-Mar-13 16:56pm
Sergey Alexandrovich Kryukov 26-Mar-13 17:23pm

Anyway, please see my answer. It has 100% of what you need (it's a pretty popular question).

1 solution

Rate this: bad
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[^],
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 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[^].


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2017
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