在 C# 中,在黑色背景(屏幕保护程序)上每 20 秒淡入和淡出图像一次,持续时间为 1 秒,最好的(资源最少的)方法是什么?

(大约 350x130 像素的图像)。

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

现在我正在对图片框使用此方法,但它太慢了:

    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;
    }
有帮助吗?

解决方案

您也许可以在MSDN上的这个例子中使用彩色矩阵像

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

其他提示

您可以使用而不是使用 Pen 和 DrawLine() 方法 位图.LockBits 直接访问图像的内存。这是一个 很好的解释 它是如何运作的。

把你的窗体上的一个定时器,并且在构造,或在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;
}

下面是我拿到这个

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top