Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:45:14+00:00 2026-06-18T09:45:14+00:00

I have C# fading out a picture box, when I click a button, and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:45:15+00:00Added an answer on June 18, 2026 at 9:45 am

    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 PictureBox control if you are using Graphic to 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 transitionTest image, so I defined a Properties_Resources_transitionTest for this.

    int MAX = 40;
    int alpha;
    bool backButtonClick;
    bool breakCheck;
    Label label1;
    Image Properties_Resources_transitionTest;
    Image image;
    

    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.

    private void storyChooser_Click(object sender, EventArgs e) {
      alpha = 0;
      backButtonClick = false;
      breakCheck = false;
      image = Properties_Resources_transitionTest;
      timer1.Start();
    }
    

    For the backButtonClick and breakCheck values, I put those inside a form’s click event handler like so:

    private void backButton_Click(object sender, EventArgs e) {
      backButtonClick = !backButtonClick;
    }
    
    private void break_Click(object sender, EventArgs e) {
      breakCheck = true;
      // easier to just write
      // timer1.Stop();
    }
    

    This Timer Tick event handler should take care of what you are trying to do. Notice the while loop has been removed, as well.

    private void timer1_Tick(object sender, EventArgs e) {
      if (!breakCheck) {
        using (Pen p = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width)) {
          using (Graphics g = Graphics.FromImage(image)) {
            g.DrawRectangle(p, 0, 0, image.Width, image.Height);
          }
        }
        if (backButtonClick) {
          alpha--;
          if (alpha == 0) {
            timer1.Stop();
          }
        } else {
          alpha++;
          if (alpha == MAX) {
            timer1.Stop();
          }
        }
      } else {
        timer1.Stop();
      }
      label1.Text = alpha.ToString();
    }
    

    UPDATE: Per question in the comment:

    If backButtonClick is true, the alpha value will decrease by 1 each time the timer ticks and stop when alpha gets to zero.

    If backButtonClick is false, alpha will increase until it reaches MAX, then stop the timer.

    Since alpha controls your Pen color’s Opacity, your image will appear to fade in and out, depending on what value you have backButtonClick set to.

    I believe that is what you are after, but I could be wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have fade-in, fade-out effect for two image views I have two
I have an element that is set to fade out after a 1500ms delay.
I have an object animating, which I would like to start fading out, after
I have a function that rotates various ads by fading in and out, but
I am having a problem fading out my subview. I have no trouble fading
I wish to have an NSView with one side of it fading out to
I have a Swing application and I'm trying to fade out the main application
I have this label that i want to fadeOut after button event clicked. I
Hi there not very proficient in coding. I have #content fading out and in
so i have this script that is simply fading p tags in and out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.