Melhor maneira de fade in / out imagem
Pergunta
O que é a melhor maneira (menos pesado recurso) a desvanecer-se uma imagem dentro e fora a cada 20 segundos, com uma duração de 1 segundo, contra um fundo preto (screensaver), em C #?
(uma imagem de 350x130px).
Eu preciso disto para um screensaver simples que vai correr em alguns computadores de baixo nível (XP).
Agora eu estou usando este método contra um pictureBox, mas é muito lento:
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;
}
Solução
Você provavelmente poderia usar uma matriz de cores, como neste exemplo no MSDN
http://msdn.microsoft.com /en-us/library/w177ax15%28VS.71%29.aspx
Outras dicas
Em vez de usar um método () Pen eo DrawLine, você pode usar Bitmap.LockBits para acessar a memória de sua imagem diretamente. Aqui está um boa explicação de como ele funciona.
Coloque um temporizador em sua forma, e no construtor, ou o Form_Load, write
timr.Interval = //whatever interval you want it to fire at;
timr.Tick += FadeInAndOut;
timr.Start();
Adicionar um método privado
private void FadeInAndOut(object sender, EventArgs e)
{
Opacity -= .01;
timr.Enabled = true;
if (Opacity < .05) Opacity = 1.00;
}
Aqui está a minha opinião sobre isso
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;
}
Também é recomendado criar outro segmento, porque isso irá congelar sua principal delas.