La meilleure façon de fade in / out l'image
Question
Quelle est la meilleure (moins lourde ressources) façon de disparaître une image et sur toutes les 20 secondes avec une durée de 1 seconde, sur un fond noir (écran de veille), en C #?
(une image à propos de 350x130px).
Je en ai besoin pour un économiseur d'écran simple qui va fonctionner sur certains ordinateurs de bas niveau (XP).
En ce moment je suis en utilisant cette méthode contre un PictureBox, mais il est trop lent:
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;
}
La solution
Vous pouvez probablement utiliser une matrice couleur comme dans cet exemple sur msdn
http://msdn.microsoft.com /en-us/library/w177ax15%28VS.71%29.aspx
Autres conseils
Au lieu d'utiliser un stylo et la méthode DrawLine (), vous pouvez utiliser Bitmap.LockBits pour accéder à la mémoire de votre image directement. Voici un bonne explication de la façon dont cela fonctionne.
Mettre une minuterie sur votre formulaire, et dans le constructeur, ou Form_Load, écrire
timr.Interval = //whatever interval you want it to fire at;
timr.Tick += FadeInAndOut;
timr.Start();
Ajoutez une méthode privée
private void FadeInAndOut(object sender, EventArgs e)
{
Opacity -= .01;
timr.Enabled = true;
if (Opacity < .05) Opacity = 1.00;
}
Voici mon avis sur cette
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;
}
Il est également recommandé de créer un autre thread parce que cela va geler votre principal.