Resolved Fade in from BLACK screen to showing a picture ?

ksor

Active member
Joined
Jul 4, 2019
Messages
33
Programming Experience
10+
I have made a form with a picturebox full screen and it works nicely !

I stop it again by clicking on the picture - working nicely too !

Now I need to let the picture FADE in from a black screen ... how can I do that in C# ?

I get a lot of hits out there with something called Unity ... don't know what it is - I want it in C#.
 
You'll have to play with the alpha channel of the image. Range from 0 to make the image transparent, to 255 to make the image opaque. Assuming that the background behind the image is black, than when the image is transparent, it'll show black, then gradually bring the image back in as the alpha approaches 255.

If your playing with a bitmap that has no alpha, the hard way to do things is to compute the luminance of each pixel and then multiply with a brightness factor. Range that brightness from 0.0 to 1.0.

You can likely take advantage of the Color Matrix. See How to: Use a Color Matrix to Transform a Single Color. Instead of playing with the RGB colors, you'll want to play with the Alpha.
 
If its a form and its full-screen? You could use the opacity of the form as a fading mechanism and run it on a windows timer. You would need a timer, and with that timer, set the timer to tick every so whatever milliseconds, and per each tick, you decrease the opacity of the form by a factor of 3 or 5 from its current opacity value and the form will eventually fade out with the image. You could also do something similar if you don't want the whole form to fade out, and only fade the image, but you will need to follow on the lines of what was posted above for that.
 
The opacity of the form is the alpha channel for the entire window. Good idea.
 
There is also another way to do it, similar to how you posted, but lets see which way the OP wants to do it first. If I understood what OP is trying to do, my way on #3 is obviously a better option for him/her.
 
I used the "Opacity -Timer" method and here is my Form code:

public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Opacity=0; this.timer1.Enabled=true; } private void Form1_Load(object sender, EventArgs e) { this.Width=Screen.PrimaryScreen.WorkingArea.Width; this.Height=Screen.PrimaryScreen.WorkingArea.Height; pictureBox1.Width=this.Width; pictureBox1.Height=this.Height; } private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { if (this.Opacity < 1) { this.Opacity=1; timer1.Enabled=false; } else { this.Close(); } } private void timer1_Tick(object sender, EventArgs e) { this.Opacity=this.Opacity+0.01; } }
 
The opacity range from the lowest starts at 0, and goes up to 100 which is the fullest.

You could also make it fade in and out using the same timer?
 
Yes as per the link above, you can use integers 0 - 100 or doubles 0.0 - 1.0 but you could improve what you have @ksor if you want it to fade in and out? What this does is, if the form is fades out, it will fade in, and vice versa if its faded in, she'll fade out for you. Try it ::
C#:
        private void RunMethod()
        {
            doBegin = true;
            timer1.Interval = 85;
            timer1.Start();
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            if (mode == 0) mode = 1;
            else mode = 0;
            RunMethod();
        }
        private int mode = 0; private bool doBegin; private double valueOpacity = 0.0;

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (doBegin == true && valueOpacity == 0.0) { valueOpacity = 0.1; mode = 0; }
            if (doBegin == true && valueOpacity == 1.0) { valueOpacity = 0.9; mode = 1; }
            if (doBegin == true && mode == 0) valueOpacity = valueOpacity + 0.1;
            if (doBegin == true && mode == 1) valueOpacity = valueOpacity - 0.1;
            if (valueOpacity <= 0.0) { timer1.Stop(); doBegin = false; this.Opacity = 0.0; valueOpacity = this.Opacity; }
            if (valueOpacity >= 1.0) { timer1.Stop(); doBegin = false; this.Opacity = 1.0; valueOpacity = this.Opacity; }
            this.Opacity = valueOpacity;
        }
        private void PictureBox1_Click(object sender, EventArgs e)
        {
            if (mode == 0) mode = 1;
            else mode = 0;
            RunMethod();
        }
Notice how in your code, you are only incrementing the Opacity with ::
C#:
private void timer1_Tick(object sender, EventArgs e) {
this.Opacity=this.Opacity+0.01;
}

Hope this helps.

Edit, just to Add : If you want to make the transition period faster, you can lower this timer1.Interval = 85; or higher the value to slow down the transition. Getting the balance is important, as the higher your interval, the more laggy your transitions looks as it increments slower.
 
Last edited:
Now I've used it for a little while from my desktop ... so I see my desktop and then the picture slowly emergies and replaces the desktop.

Maybe it would be better to start with a totally black screen and then the picture slowly shows up - I've tried giving the Form a background color of BLACK but it wont work because it's the FORM that has the Opacity property and not the picturebox.

How is this done ?
 
I don't follow, please explain and be a detailed as possible regarding what it is you're trying to do. What I already provided you makes the whole window transparent bar some borders...

What do you mean by this :
Now I need to let the picture FADE in from a black screen ... how can I do that in C# ?
This implied you want the form black and you want to fade in the image which sits on the form? Right or wrong?
I get a lot of hits out there with something called Unity ... don't know what it is - I want it in C#.
I am not familiar with anything to help regarding winforms and transparency. The only Unity i know is a game engine called unity3D and I use it for making open world/FPS games for my own customers. That wouldn't help you here.
I've tried giving the Form a background color of BLACK but it wont work because it's the FORM that has the Opacity property and not the picturebox.
Why does the form need to be black for you?

Yes, the form does have the opacity, and not the picture box. But the form does have a Transparent Key Property - and a panel can be set as a transparent control or you can set a panels back color to be the same as your Forms Transparent Key color. Maybe looking into that will direct you in a different direction.

But what you really need to do is :
  1. Explain in detail and precisely what you are trying to do. (I am encouraging elaboration)
  2. Explain in detail what your desired outcome should be.
  3. Explain in detail how the sample I posted above does not satisfy your needs?
Please be specific.
 
I explained in my first posting what I wanted ...

Now that I HAVE some working code it COULD be satisfying working as I describe in my posting #12

Compared to my first posting, it starts out with showing my desktop ... what I wanted in my first posting was starting out showing a BLACK screen. - that what I'm asking now ... it's that simple !

I like the old Occams Razor ... a early version of the KIS-principle ... Keep It Simple ... I don't think you code is in line with that - sorry to say.
 
My code is in line with what I believed you were asking for in the beginning. You and your post are great examples of why I generally don't provide code samples right off the bat and that's why I tend to ask more questions instead of flinging irrelevant answers at people. That is why it is important for you to be as descriptive as possible with us, so that we can understand what you want to do; why you want to do it; and how you plan on doing it; and only then we can promptly give you a fast answer once that understanding is clear. Rather than going around the houses throwing code at you for something that is misinterpreted as something you want, when it's actually not. Don't assume we know what you're thinking of doing and why, because we don't. We are not mind readers and if we don't ask for more information, it becomes harder to help you.

Its time consuming enough for us to take time out of our day and type something up for somebody who doesn't want the code we wrote because they never bothered to put more information into explaining elaborately on their opening topic, in order for us to have a clearer picture of what it is you're trying to do and why. And thus misleads us, or in this case me when that information is not clear. It was assumed that you want a fading picture which is docked into your form. Since the picturebox lacks the same functional property as the windows form opacity property offers, it is still possible to do it but more complicated, but it is easier to do it by way of what I gave you already. Alternatively this is easiest done in WPF.

Further, its common courtesy to thank someone who tries to help you, after all you are availing of a free service and free advice and sometimes free code samples too, and for absolutely nothing. Spend a little more time on post 13 before dismissing what I gave you as being not in-line with what you're asking. In my opinion; showing a black screen off the bat without fading the form in is rather ugly and not necessary, only to roll a picture up over that very ugly black form seems rather pointless. But that's just my opinion, and my opinion is based on the fact that I don't know why you are doing what you're doing or for what reason. From a visual aspect, it would be cooler if the form would fade in too and not just the control. You actually have a working solution on post 13, but I am sure you can work it out by yourself if you can read between the lines, just as you expect us too. (y)

It's bad practice to assume what one programmer wants to do, as no two ideologies hold the same theory.

Good luck with the project my friend.
 
Back
Top Bottom