Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hey, I've tried using both this batch of code and another batch of code for fading images in and out. Both manage to successfully fade the image out, but I cannot get the second image to fade back in. I believe this has to do with both batches drawing rectangles over the image of either lesser and lesser opacity or greater and greater opacity.

I've tried both of those codes within nested if statements that determine whether the image present fades out and when to bring the new image in to start fading it up.
If anyone could point me in the proper direction for changing the opacity of the image back up that would be greatly appreciated.

Here's my code for refrence:

C#
private void timer1_Tick(object sender, EventArgs e)
        {
           
            //Runs checks to determine if first image should fade out.
            if (imageChange == true)
            {
                if (breakCheck2 == false)
                {

                    //fades out the image.
                    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;
                        if (alpha == 40)
                        {
                            breakCheck = true;
                        }
                    }
                }
            }
            
            //runs check to see if second image should fade up.
            if (breakCheck == true)
            {
                if (alpha-- > 0 )
                {
                    //grads second image when applicable.
                    if (imageChange == true)
                    {
                        pictureBox1.Image = Properties.Resources.transitionTest;
                        pictureBox1.Invalidate();
                        imageChange = false;
                    }
                    
                    //Should fade in second image, but instead fades it out.
                    Image image = pictureBox1.Image;
           

                    float opacityvalue = opacity = +.5F;
                    pictureBox1.Image = ChangeOpacity(image, opacityvalue);
                    
                   
                    
                }
                if (alpha == 0)
                {
                    breakCheck = false;
                    breakCheck2 = false;
                    imageChange = true;
                    opacity = 0;
                    timer1.Stop();
                }
               
            }
Posted
Comments
Sergey Alexandrovich Kryukov 6-Feb-13 21:36pm    
Start from not using PictureBox at all. Why would you ever need it?
—SA

1 solution

Your problem is because you are using pictureBox1.Image as both the source and the target of the exercise. Once you have updated the image once that's your new starting point and the original image is lost.

I managed to get the effect you're looking for by using the first[^] of the links you suggested by having a 2nd pictureBox and calling the ChangeOpacity Function with the image from that.
pictureBox1.Image = ImageUtils.ImageTransparency.ChangeOpacity(pictureBox2.Image, opacityvalue);

You'll notice that Ravi uses a file as his source image in the article - same effect.
 
Share this answer
 
Comments
Joezer BH 10-Feb-13 3:09am    
5+

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