I have C# fading out a picture box, when I click a button, and after the fade out the same picture box will fade in the next image. Currently, I have the fade out code placed both in the fade out and fade in conditions. (I still need to rewrite the code for fade in.) Once the code hits the logic for the second image, the code just loops through until the conditions turn up false and exits without changing the display. How can I correct my code to get the same effect going on the second image? Also, if anyone knows how to rewrite the fade out logic for fade in please let me know.
Variables defined at top:
int alpha = 0;
bool backButtonClick = false;
bool breakCheck = false;
Button’s logic snippet:
private void storyChooser_Click(object sender, EventArgs e)
{
switch (userChoice)
{
case Choice.Son:
transitions();
if (alpha == 0)
{
pictureBox1.Image = Properties.Resources.test;
timer1.Start();
}
break;
}
The timer:
private void timer1_Tick(object sender, EventArgs e)
{
if (backButtonClick == true)
{
timer1.Stop();
alpha = 0;
backButtonClick = false;
}
if (alpha++ < 40)
{
Image image = pictureBox1.Image;
using (Graphics g = Graphics.FromImage(image))
{
Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
g.DrawRectangle(pen, -1, -1, image.Width, image.Height);
g.Save();
}
pictureBox1.Image = image;
}
else
{
timer1.Stop();
}
if (alpha == 40 && breakCheck == false)
{
pictureBox1.Image = Properties.Resources.transitionTest;
timer1.Start();
while (alpha-- > 0)
{
Image image = pictureBox1.Image;
using (Graphics g = Graphics.FromImage(image))
{
Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
g.DrawRectangle(pen, -1, -1, image.Width, image.Height);
g.Save();
}
pictureBox1.Image = image;
label1.Text = alpha.ToString();
}
breakCheck = true;
}
label1.Text = alpha.ToString();
}
I’m running through the while loop, towards the bottom of the timer, without updating any of the graphics.
Thanks.
It looks like your second fadein condition has a while loop in it.
That’s going to make your control go from one side to the other before your next timer tick event.
I had to re-write your code so I could get a better feel for it, though.
Why are you using a
PictureBoxcontrol if you are usingGraphicto write directly to the screen?Here is my horrible mangling of your code. I hope it doesn’t offend.
I declared all of the variables up front so VS would not complain about bad definitions.
I also did not have a
transitionTestimage, so I defined aProperties_Resources_transitionTestfor this.Initialized stuff below and kicked it off. I don’t know what your enumerated type means or that
transition()method was for that you called.For the
backButtonClickandbreakCheckvalues, I put those inside a form’s click event handler like so:This Timer Tick event handler should take care of what you are trying to do. Notice the
whileloop has been removed, as well.UPDATE: Per question in the comment:
If
backButtonClickis true, thealphavalue will decrease by 1 each time the timer ticks and stop whenalphagets to zero.If
backButtonClickis false,alphawill increase until it reachesMAX, then stop the timer.Since
alphacontrols yourPencolor’s Opacity, your image will appear to fade in and out, depending on what value you havebackButtonClickset to.I believe that is what you are after, but I could be wrong.