我有一個WriteableBitmap圖像,我只想在buttonclick事件中僅顯示它的綠色組件。在可寫位圖圖像中僅顯示ARGB中的綠色組件顏色
早些時候我試圖先把它轉換成灰度,然後我希望能在這之後只顯示綠色組件,但無論如何我都無法破解它。
下面就是我目前正在爲將圖像轉換爲灰度圖像的代碼:
public static WriteableBitmap ToGrayScale(WriteableBitmap bitmapImage) {
for (var y = 0; y < bitmapImage.PixelHeight; y++) {
for (var x = 0; x < bitmapImage.PixelWidth; x++) {
var pixelLocation = bitmapImage.PixelWidth * y + x;
var pixel = bitmapImage.Pixels[pixelLocation];
var pixelbytes = BitConverter.GetBytes(pixel);
var bwPixel = (byte)(.299 * pixelbytes[2] + .587 * pixelbytes[1] + .114 * pixelbytes[0]);
pixelbytes[0] = bwPixel;
pixelbytes[1] = bwPixel;
pixelbytes[2] = bwPixel;
bitmapImage.Pixels[pixelLocation] = BitConverter.ToInt32(pixelbytes, 0);
}
}
return bitmapImage;
}
我收到此錯誤: 「Windows.UI.Xaml.Media.Imaging.WriteableBitmap」呢不包含'像素'的定義,也沒有擴展方法'像素'接受類型'Windows.UI.Xaml.Media.Imaging.WriteableBitmap'的第一個參數可以找到(你是否缺少using指令或程序集引用?)
有些幫助會非常感謝我。謝謝。
bitmapImage.Pixels [pixelLocation] = BitConverter.ToInt32(pixelbytes,0);我在這條線上面臨着問題,沒有像素的參考。 –
對不起,我忽略了。如果你的灰度轉換不起作用,你能否用錯誤信息更新你的問題? –
最有可能的是,你的問題是你正在轉換爲'Int32'。像素值通常是「uint8」。但我不熟悉C# –