2016-12-05 46 views
0

即時結構已經很長一段時間了。我試圖改變圖像的特定像素上的alpha通道。我想將圖像轉換爲只有alpha通道的二維數組。我在Android中得到了這個工作,但不知道如何在UWP中工作。UWP - 改變特定像素的Alpha在uwp

Android版本

short[][] dotMapArray; 
short[][] heatMapArray; 
int[] scolorGradiant; 

void init() { 
    Bitmap dotBitmap = loadImageFromResources(DOT); 
    Bitmap gradiantBitmap = loadImageFromResources(SPECTRUMPIC); 

    // initializing the 'dot' for the points on the heatmap 
    dotMapArray = new short[dotBitmap.getWidth()][dotBitmap.getHeight()]; 
    print(true, "Converting Dot image to black image"); 
    for (int x = 0; x < dotBitmap.getWidth(); x++) { 
     for (int y = 0; y < dotBitmap.getHeight(); y++) { 
      dotMapArray[x][y] = (short) (Color.alpha(dotBitmap.getPixel(x, y))); 
      if (dotMapArray[x][y] > 255) 
       dotMapArray[x][y] = 255; 
     } 
    } 
    // initializing the color gradiant 
    scolorGradiant = new int[gradiantBitmap.getHeight()]; 
    int gradiant, pixelColor; 
    for (int y = 0; y < scolorGradiant.length; y++) { 
     gradiant = gradiantBitmap.getPixel(0, y); // color 
     pixelColor = Color.argb(90, Color.red(gradiant), Color.green(gradiant), Color.blue(gradiant)); 
     scolorGradiant[y] = pixelColor; 
    } 

} 

如何在UWP做到這一點? 任何人都可以幫助我嗎?

回答

1

這裏的一個循環,檢查第一信道(BGRA)如果藍色= 255,那麼所有信道去0

 var pixels = yourWriteableBitmap.PixelBuffer.ToArray(); 
     for (int i = 0; i < pixels.Length; i += 4) 
     { 
      if (pixels[i] == 255) 
      { 
       pixels[i] = 0; 
       pixels[i + 1] = 0; 
       pixels[i + 2] = 0; 
       pixels[i + 3] = 0; 
      } 
     } 
     using (var stream = yourWriteableBitmap.PixelBuffer.AsStream()) 
     { 
      await stream.WriteAsync(pixels, 0, pixels.Length); 
     }