0

アイデアは、C# を使用して Visual Studio 2010 で Windows フォーム アプリケーションを構築することです。

ユーザーがボタンを押すと、プログラムは一連の操作を実行します。

プログレスバーを使用する代わりに画像を使用して進行状況を表示することはできますか?

つまり、画像が見えなくなり始め、プログラムが進行するにつれて、画像がますます見えるようになるという考えです。

0% - invisible
50% - half transparent
100% - visible

PictureBox を表示または非表示 (PictureBox.Visible = true または false;) に切り替えることができることは知っていますが、その間に表示する方法はありますか?

どんなアイデアでも大歓迎です。

4

2 に答える 2

0

winforms で画像を操作するのは遅いので、できるだけ少なくしてください:

public Bitmap ImageFade( Bitmap sourceBitmap, byte Transparency)
    {
        BitmapData sourceData = sourceBitmap.LockBits(new Rectangle(0, 0,
                                    sourceBitmap.Width, sourceBitmap.Height),
                                    ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

        byte[] pixelBuffer = new byte[sourceData.Stride * sourceData.Height];
        byte[] resultBuffer = new byte[sourceData.Stride * sourceData.Height];

        Marshal.Copy(sourceData.Scan0, pixelBuffer, 0, pixelBuffer.Length);

        sourceBitmap.UnlockBits(sourceData);

        byte blue = 0;
        byte green = 0;
        byte red = 0;
        byte a = 0;

        int byteOffset = 0;

        for (int offsetY = 0; offsetY <
             sourceBitmap.Height; offsetY++)
        {
            for (int offsetX = 0; offsetX <
                 sourceBitmap.Width; offsetX++)
            {
                blue = 0;
                green = 0;
                red = 0;
                a = 0;

                byteOffset = offsetY *
                                sourceData.Stride +
                                offsetX * 4;

                blue += pixelBuffer[byteOffset];
                green += pixelBuffer[byteOffset + 1];
                red += pixelBuffer[byteOffset + 2];
                a += Transparency;//pixelBuffer[byteOffset + 3];

                resultBuffer[byteOffset] = blue;
                resultBuffer[byteOffset + 1] = green;
                resultBuffer[byteOffset + 2] = red;
                resultBuffer[byteOffset + 3] = a;
            }
        }

        Bitmap resultBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);

        BitmapData resultData = resultBitmap.LockBits(new Rectangle(0, 0,
                                resultBitmap.Width, resultBitmap.Height),
                                ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

        Marshal.Copy(resultBuffer, 0, resultData.Scan0, resultBuffer.Length);
        resultBitmap.UnlockBits(resultData);

        return resultBitmap;
    }

別の答えが SetPixel を使用していることに注意してください。可能であれば、その機能を使用しないでください。基になるバイトストリームを編集する方がはるかに高速です。これはハードウェア アクセラレーションではないためまだ遅いですが、いくつかの優れていないオプションの中で最も優れています。

この関数はさらに最適化される可能性がありますが、それは読者の演習として残します

于 2013-09-23T20:28:58.963 に答える
0

画像のアルファ コンポーネントはいつでも調整できます。

void SetImageProgress(float percent, Bitmap img)
{
   int alpha = (int)(percent / 100.0f * 255.0f);
   alpha &= 0xff;
   for(int x = 0; x < img.Width; x++)
   {
      for(int y = 0; y < img.Height; y++)
      {
         Color c = img.GetPixel(x, y);
         c = Color.FromArgb(alpha, c.R, c.G, c.B);
         img.SetPixel(x, y, c);
      }
   }
}
于 2013-09-23T20:17:08.887 に答える