簡體   English   中英

需要幫助以了解此示例代碼

[英]Need help in understanding this example code

private void SetAlpha(string location)
{
    //bmp is a bitmap source that I load from an image
    bmp = new BitmapImage(new Uri(location));
    int[] pixels = new int[(int)bmp.Width * (int)bmp.Height];
    //still not sure what 'stride' is.  Got this part from a tutorial
    int stride = (bmp.PixelWidth * bmp.Format.BitsPerPixel + 7)/8;

    bmp.CopyPixels(pixels, stride, 0);
    int oldColor = pixels[0];
    int red = 255;
    int green = 255;
    int blue = 255;
    int alpha = 0;
    int color = (alpha << 24) + (red << 16) + (green << 8) + blue;

    for (int i = 0; i < (int)bmp.Width * (int)bmp.Height; i++)
    {
        if (pixels[i] == oldColor)
        {
            pixels[i] = color;
        }
    }
        //remake the bitmap source with these pixels
        bmp = BitmapSource.Create(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, bmp.Format, bmp.Palette, pixels, stride);
    }

}

您能幫我解釋一下這段代碼嗎? coloroldColor是什么意思?

此代碼用RGBA位圖中的新顏色替換和oldColor。

新顏色已滿-完全不透明的白色。 舊顏色取自第一個像素。 許多圖標和面具

跨度是每個掃描行/行有多少個字節。

錯誤:

1)bmp.CopyPixels(pixels,stride,0); 僅復制第一行。 它應該是bmp.CopyPixels(pixels,stride * bmp.Height,0);

2)保證RGB顏色的特定布局。 它不會檢查“新BitmapImage”,“新int []”和BitmapSource.Create的結果

3)函數名稱錯誤。

暫無
暫無

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

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