Second timer.Start() isn't being triggered by

2019-08-18 04:04发布

问题:

So I have a small logo in the lower right corner of a Form that I want to fade in and out at a preset speed, about 6 seconds per fade. I have tried a few different methods but I can never get the picture to fade back in again once the first timer has finished. Here's my code for the the 2 timers and their respective tick methods.

EDIT The declarations for the timers included now.

        Timer fade = new Timer();
        Timer fade2 = new Timer();


                fade.Interval = (200);
                fade.Tick += new EventHandler(fade_Tick);


                fade2.Interval = (200);
                fade2.Tick += new EventHandler(fade_Tick_Two);

                fade.Start();

private void fade_Tick(object sender, EventArgs e)
        {
            if (alpha < 255)
            {
                image = picboxPic.Image;
                using (Graphics g = Graphics.FromImage(image))
                {
                    Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
                    g.DrawLine(pen, -1, -1, image.Width, image.Height);
                    g.Save();
                }
                picboxPic.Image = image;
                this.Invalidate();
                alpha++;
            }
            else
            {
                fade.Stop();
                fade2.Start();
            }


        }
private void fade_Tick_Two(object sender, EventArgs e)
        {

            if (alpha > 0)
            {
                image = picboxPic.Image;
                using (Graphics g = Graphics.FromImage(image))
                {
                    Pen pen = new Pen(Color.FromArgb(alpha, 255, 255, 255), image.Width);
                    g.DrawLine(pen, -1, -1, image.Width, image.Height);
                    g.Save();
                }
                picboxPic.Image = image;
                this.Invalidate();
                alpha--;
            }
            else
            {
                fade2.Stop();
                fade.Start();
            }

        }

Any ideas as to why the second timer won't start? I've used breakpoints and the alpha level does reach 255 but then it doesn't trigger the second Tick event.

回答1:

The method described in the link I quoted works for me:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        test();
    }
    System.Timers.Timer fade = new System.Timers.Timer(50);
    System.Timers.Timer fade2 = new System.Timers.Timer(50);
    Image originalImage = Image.FromFile(@"D:\kevin\Pictures\odds&Sods\kitchener.jpg");

    int alpha = 100;

    void test()
    {
        fade.Elapsed +=new System.Timers.ElapsedEventHandler(fade_Tick);
        fade2.Elapsed+=new System.Timers.ElapsedEventHandler(fade_Tick_Two);
        fade.Start();

    }
    delegate void timerDelegate(object sender, EventArgs e);
    private void fade_Tick(object sender, EventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new timerDelegate(fade_Tick), sender, e);
            return;
        }
        if (alpha >= 0)
        {
            picboxPic.Image =  SetImgOpacity(originalImage, alphaToOpacity(alpha));
            picboxPic.Invalidate();
            alpha--;
        }
        if (alpha < 0)
        {
            alpha = 0;
            fade.Stop();
            fade2.Start();
        }
    }
    private void fade_Tick_Two(object sender, EventArgs e)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new timerDelegate(fade_Tick_Two), sender, e);
            return;
        }
        if (alpha <= 100)
        {
            picboxPic.Image = SetImgOpacity(originalImage, alphaToOpacity(alpha));
            picboxPic.Invalidate();
            alpha++;
        }
        if (alpha > 100)
        {
            alpha = 100;
            fade2.Stop();
            fade.Start();
        }
    }

    float alphaToOpacity(int alpha)
    {
        if (alpha == 0)
            return 0.0f;

        return (float)alpha / 100.0f;
    }

    //Setting the opacity of the image
    public static Image SetImgOpacity(Image imgPic, float imgOpac)
    {
        Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
        Graphics gfxPic = Graphics.FromImage(bmpPic);

        ColorMatrix cmxPic = new ColorMatrix();
        cmxPic.Matrix33 = imgOpac;
        ImageAttributes iaPic = new ImageAttributes();
        iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
        gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
        gfxPic.Dispose();

        return bmpPic;
    }
}

The code is a bit crude and you will need to take care of the dispose when you close the form, but it fades up and and down and the rest can easily be taken care of - I also made it quicker for testing 'cos life's short :-)