I'm sending to my method different images and I want insert some effect to this change.
How can I fade in and fade out images?
private void ShowImage(Image image, ImageLayout imageLayout, int numberOfSeconds)
{
try
{
if (this.image_timer != null)
this.KillImageTimer();
this.customer_form.DisplayImage(image, imageLayout);
this.image_timer = new Timer();
this.image_timer.Tick += (object s, EventArgs a) => NextImage();
this.image_timer.Interval = numberOfSeconds* 1000;
this.image_timer.Start();
}
catch
{
//Do nothing
}
public void DisplayImage(Image image, ImageLayout imageLayout)
{
panel1.BackgroundImage = image;
panel1.BackgroundImageLayout = imageLayout;
}
There are no built-in fading transitions in
Winforms
.So you will need to write one yourself.
The simplest one I can think of uses a second
Panel
, that is layered upon the first one and in fact needs to be inside the firstPanel
or else the transparency effect won't work..Here is the setup, using two
Panels
:For the fading animation I use a
Timer
. This is a quick code example:I start it in a Button, on which I also show the current state:
As you can see I use two speeds you can set: One to control the speed of the
Timer
and one to control the steps by which the transparency changes on eachTick
.The effect is created by simply changing the
Color
from theBackgroundColor
of the imagePanel
to fully transparent and back, waiting in between for a specified number of seconds.And the end of the effect I call a function
changeImage()
to change the images. If this function returnsfalse
theTimer
is stopped for good..I'm pretty sure this could be written in a cleaner and more elegant way, but as it is it seems to work..
Update
Here is a sample implementation for
changeImage
:It assumes two class level variables: a
List<string> imageFiles
filled with file names of images for a slide-show and anint index = 0
.