画像をフェードイン/フェードアウトする最良の方法
質問
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;
}
解決
おそらく、msdn のこの例のようにカラー マトリックスを使用できるでしょう。
http://msdn.microsoft.com/en-us/library/w177ax15%28VS.71%29.aspx
他のヒント
Pen と DrawLine() メソッドを使用する代わりに、次のようにすることができます。 ビットマップ.ロックビット 画像のメモリに直接アクセスします。ここにあります 良い説明 それがどのように機能するかについて。
フォームにタイマーを置き、コンストラクター、または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;
}
メインのスレッドが凍結してしまうため、別のスレッドを作成することもお勧めします。
所属していません StackOverflow