如何获得的WinRT像素的RGB值(How to get RGB value of a pixel

2019-07-05 17:24发布

我试图获得的WinRT应用程序的每个像素的RGB值。 我可以访问包含字节数组PixelData ,但我不知道如何与工作,所以我怎么能提取从字节数组的RGB信息?

var bd = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var pd = await bd.GetPixelDataAsync();
var tempBuffer = pd.DetachPixelData();
var PixelMatrix = new byte[bd.PixelWidth, bd.PixelHeight];
// how do I get the RGB value for PixelMatrix[0,0]?

Answer 1:

因为你有一个RGB图像, tempBuffer[k + 0]是红色通道, tempBuffer[k + 1]是绿色通道,和tempBuffer[k + 2]是蓝色通道,即tempBuffer是一维数组。 如果你遍历所有像素,这样做的伪代码如下:

for i = 0 to height - 1
    for j = 0 to width - 1
        k = (i * width + j) * 3
        r, g, b = tempBuffer[k + 0], tempBuffer[k + 1], tempBuffer[k + 2]


Answer 2:

由于Marshal类上没有WinRT的-进行最高效的方法是使用一个SafeMemoryMappedViewHandle (SafeBuffer)。

这种方法还可以处理与多字节部件的PixelFormats而不需要使用一个BinaryReader在和由组分(RGBA16与每个分量16位)读出它的组成部分。 找出像素格式是使用解码器的BitmapPixelFormat属性,并使用适当声明的结构。

    // declare more of these appropriately laid
    // out structures for different pixel formats
    struct RGBA16
    {
        public uint R;
        public uint G;
        public uint B;
        public uint A;
    }

    struct RGBA8
    {
        public byte R;
        public byte G;
        public byte B;
        public byte A;
    }

    struct BRGA8
    {
        public byte B;
        public byte G;
        public byte R;
        public byte A;
    }
    ...

    var handle = GCHandle.Alloc(tempBuffer /* the raw byte[] */, GCHandleType.Pinned);
    try
    {
        var ptr = handle.AddrOfPinnedObject();
        var safeBuffer = new SafeMemoryMappedViewHandle(true /* I believe DetachPixelData returns a copy? false otherwise  */)
        safeBuffer.SetHandle(ptr);

        #if STREAM_PROCESSING
            // pixel by pixel
            int offset = 0;
            for (int i = 0; i < width * height; i++)
            {
                var pixel = safeBuffer.Read<RGBA16>(offset);
                offset += RGB24bpp.Size;
            }
        #else
            // Read it all in at once - this makes a copy
            var pixels = new RGBA16[width * height];
            safeBuffer.ReadArray<RGBA16>(0, pixels, 0, width * height);
        #endif
    }
    finally
    {
        safeBuffer.Dispose();
        handle.Free;
    }

注意 :此方法也可以用于需要Marshal.PtrToStructure或一些这样的等效上的WinRT任何操作的替代。



文章来源: How to get RGB value of a pixel in WinRT