views:

245

answers:

3

What is the best (least resource heavy) way to fade an image in and out every 20 seconds with a duration of 1 second, against a black background (screensaver), in C# ?

(an image about 350x130px).

I need this for a simple screensaver that's going to run on some low level computers (xp).

Right now I'm using this method against a pictureBox, but it is too slow:

    private Image Lighter(Image imgLight, int level, int nRed, int nGreen, int nBlue)
    {
        Graphics graphics = Graphics.FromImage(imgLight);
        int conversion = (5 * (level - 50));
        Pen pLight = new Pen(Color.FromArgb(conversion, nRed,
                             nGreen, nBlue), imgLight.Width * 2);
        graphics.DrawLine(pLight, -1, -1, imgLight.Width, imgLight.Height);
        graphics.Save();
        graphics.Dispose();
        return imgLight;
    }
+1  A: 

You could probably use a Color Matrix like in this example on msdn

http://msdn.microsoft.com/en-us/library/w177ax15%28VS.71%29.aspx

John Boker
A: 

Put a Timer on your form, and in the constructor, or the Form_Load, write

    timr.Interval = //whatever interval you want it to fire at;
    timr.Tick += FadeInAndOut;
    timr.Start();

Add a private method

private void FadeInAndOut(object sender, EventArgs e)
{
    Opacity -= .01; 
    timr.Enabled = true;
    if (Opacity < .05) Opacity = 1.00;
}
Charles Bretana
You should ideally use two timers. One would be set to 20 seconds, and would trigger a fade by starting the second timer. The second timer would be set to a small time (e.g. 0.1s) and would increment/decrement opacity until the fade is finished and then would Stop() itself. THat way, you don't have a high-frequency timer running continuously. Note that Forms timers are notoriously unreliable, so you may not get a very smooth fade using them - but for what you want they'll probably be ok most of the time.
Jason Williams
Interesting, but how is two timers, one of which is running more ore less continuously at high frequency, better than a single timer at the same high frequency ?
Charles Bretana
+1  A: 

Instead of using a Pen and the DrawLine() method, you can use Bitmap.LockBits to access the memory of your image directly. Here's a good explanation of how it works.

Andy West