簡體   English   中英

使圖像在窗體上緩慢可見

[英]Make Image Slowly Visible on Form

這個想法是使用C#在Visual Studio 2010中構建Windows窗體應用程序。

用戶按下按鈕時,程序將運行一系列操作。

是否可以使用圖像顯示進度而不是使用進度條?

因此,想法是圖像將開始變得不可見,並且隨着程序的進行,圖像變得越來越可見。

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

我知道您可以將PictureBox切換為可見(PictureBox.Visible = true或false;),但是是否可以在兩者之間切換?

任何想法都值得贊賞。

您始終可以調整圖像的Alpha分量:

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

在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。 盡可能避免使用該功能。 編輯底層字節流的速度要快得多,但由於不是硬件加速的,所以它仍然很慢,但這是幾個不理想的選擇中最好的

可以進一步優化此功能,但我留給讀者練習

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM