Click here to Skip to main content
13,002,676 members (63,717 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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:

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 6-Feb-13 15:11pm
Comments
   
Start from not using PictureBox at all. Why would you ever need it?
—SA

1 solution

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

Solution 1

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.
  Permalink  
Comments
Edo Tzumer 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170626.1 | Last Updated 9 Feb 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