2010-08-25 79 views
1
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是什麼意思?需要幫助瞭解此示例代碼

+0

那是什麼語言?什麼是操作系統? – Dummy00001 2010-08-25 19:32:57

+0

WPF是Windows Presentation Foundation的一個框架,用於爲Windows創建客戶端應用程序,因此操作系統應該是WINDOWS 是.NET的語言嗎? – 2010-08-25 19:40:17

回答

5

該代碼將RGBA位圖中的新顏色替換爲oldColor。

新顏色是全 - copmletely不透明的白色。舊顏色是從第一個像素中提取的。很多圖標和麪具都做

步幅是每個掃描行/行有多少字節。

錯誤:

1)bmp.CopyPixels(像素,步幅,0);只複製第一行。它應該是bmp.CopyPixels(像素,步長* bmp.Height,0);

2)它推測RGB顏色的特殊佈局。它不會檢查「新的BitmapImage」「new int []」和BitmapSource.Create的結果。3)該函數的名稱是錯誤的。

+1

+1只是因爲「功能的名稱是錯誤的。」好的答案,但。 – 2010-08-25 19:42:24