Fading out an image with transparency in WinForms UI (.NET3.5)

后端 未结 2 394
猫巷女王i
猫巷女王i 2021-01-02 18:17

The application: I am writing a little game that would teach the user to read music notes. The game play is very simple. The app displayes a note and my lit

2条回答
  •  孤独总比滥情好
    2021-01-02 18:35

    As dylantblack says, WPF gives you better tools to do this. If you choose to use Windows forms, here's a simple approach using a timer that fades the image out. Set up a timer with whatever frequency you like. Start the timer, increment alpha every time through, and draw white or whatever your form color is with increasing alpha channel value.

    int alpha = 0;
    

    ...

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (alpha++ < 255)
        {
            Image image = pictureBox1.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();
            }
            pictureBox1.Image = image;
        }
        else
        {
            timer1.Stop();
        }
    }
    

提交回复
热议问题