简体   繁体   English

在可写位图图像中仅显示ARGB中的绿色分量颜色

[英]Showing only Green component color among ARGB in a writeablebitmap image

I have a WriteableBitmap image and I want to show only green component of it on a buttonclick event. 我有一个WriteableBitmap图像,并且我想在buttonclick事件中仅显示其绿色部分。

Earlier I was trying to first convert it to Grayscale and then i was hoping that i would be able to show only Green component after this,But anyhow i am not able to crack that. 早些时候我试图先将其转换为灰度,然后希望此后仅显示绿色分量,但是无论如何我都无法破解。

Below is the code what i am currently trying for converting the image to a Grayscale image: 下面是我目前正在尝试将图像转换为灰度图像的代码:

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

I am getting this error: 'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' does not contain a definition for 'Pixels' and no extension method 'Pixels' accepting a first argument of type 'Windows.UI.Xaml.Media.Imaging.WriteableBitmap' could be found (are you missing a using directive or an assembly reference?) 我收到此错误:“ Windows.UI.Xaml.Media.Imaging.WriteableBitmap”不包含“像素”的定义,也没有扩展方法“像素”接受类型为“ Windows.UI.Xaml.Media”的第一个参数。可以找到Imaging.WriteableBitmap”(是否缺少using指令或程序集引用?)

Some help would be very grateful to me.Thank you. 一些帮助将非常感激我。谢谢。

You have to copy the 2nd channel of the image: 您必须复制图像的第二个通道:

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 greenChannel = (byte)(pixelbytes[1]);
            pixelbytes[0] = greenChannel;
            pixelbytes[1] = greenChannel;
            pixelbytes[2] = greenChannel;
            bitmapImage.Pixels[pixelLocation] = BitConverter.ToInt32(pixelbytes, 0);
        }
    }

    return bitmapImage;
}

(In this code I am assuming that your gray conversion is correct. Windows has the tendency to store RGB images as BGR in memory. I am not sure where the alpha channel goes. You should check that with a test image, which has regions of pure colours.) (在此代码中,我假设您的灰度转换正确。Windows倾向于将RGB图像作为BGR存储在内存中。我不确定alpha通道的位置。您应该使用测试图像进​​行检查,该图像的区域为纯色。)

As noted, Windows.UI.Xaml.Media.Imaging.WriteableBitmap does not have a Pixels property: it has a PixelBuffer which returns the pixels as an IBuffer. 如前所述,Windows.UI.Xaml.Media.Imaging.WriteableBitmap没有Pixels属性:它具有PixelBuffer ,该像素缓冲区将像素作为IBuffer返回。

You can access the IBuffer with a DataReader & DataWriter or by converting to a stream with the AsStream extension method or to to a byte[] with the ToArray extension method. 您可以使用DataReader&DataWriter或通过使用AsStream扩展方法转换为流或使用ToArray扩展方法转换为byte []来访问IBuffer。 Once you have a stream or array it's straightforward to loop through, convert the bytes to green or grayscale, and set the modified pixels back. 一旦有了流或数组,就可以直接遍历,将字节转换为绿色或灰度,然后将修改后的像素设置回去。

My colleague Jeff demonstrates the grayscale conversion via AsStream in his blog entry How to: Convert an Image to Grayscale 我的同事Jeff在他的博客文章中演示了通过AsStream进行灰度转换的方法:如何将图像转换为灰度

The open source WriteableBitmapEx project also adds its own extensions to WriteableBitmap to access the PixelBuffer. 开源WriteableBitmapEx项目还向WriteableBitmap添加了自己的扩展名,以访问PixelBuffer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM