2009-12-01 39 views
1

在C#中,对于黑色背景(屏幕保护程序),每20秒持续1秒钟淡入淡出图像的最佳方式是什么?淡入/淡出图像的最佳方式

(约350x130px的图片)。

我需要一个简单的屏幕保护程序,它将在一些低级别计算机上运行(xp)。

现在,我对使用一个PictureBox这种方法,但实在是太慢了:

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; 
    } 

回答

0
你也许可以使用彩色矩阵状

将定时器放在您的表单上,并在构造函数或Form_Load中,写入

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

添加一个私有方法

private void FadeInAndOut(object sender, EventArgs e) 
{ 
    Opacity -= .01; 
    timr.Enabled = true; 
    if (Opacity < .05) Opacity = 1.00; 
} 
+0

你应该最好使用两个定时器。一会被设置为20秒,并通过启动第二个计时器来触发淡入淡出。第二个计时器将被设置为一个很短的时间(例如0.1s),并将增加/减少不透明度,直到淡入淡出完成,然后停止()本身。那么,你没有连续运行的高频计时器。请注意,Forms定时器是臭名昭着的不可靠的,所以使用它们可能不会很顺利地淡入淡出 - 但是对于你想要的东西,大多数情况下它们可能都可以。 – 2009-12-01 22:44:28

+0

有趣的是,两个定时器如何,其中一个定时器在高频率下连续运行更少,优于同一个高频率下的单个定时器? – 2009-12-01 23:40:21

0

这里是我拿到这个

private void animateImageOpacity(PictureBox control) 
    { 
     for(float i = 0F; i< 1F; i+=.10F) 
     { 
      control.Image = ChangeOpacity(itemIcon[selected], i); 
      Thread.Sleep(40); 
     } 
    } 

    public static Bitmap ChangeOpacity(Image img, float opacityvalue) 
    { 
     Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image 
     Graphics graphics = Graphics.FromImage(bmp); 
     ColorMatrix colormatrix = new ColorMatrix {Matrix33 = opacityvalue}; 
     ImageAttributes imgAttribute = new ImageAttributes(); 
     imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); 
     graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute); 
     graphics.Dispose(); // Releasing all resource used by graphics 
     return bmp; 
    } 

它也建议创建另一个线程,因为这将会冻结您的主要原因之一。