Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UWP应用程序中将遮罩应用于位图图像_C#_Image Processing_Bitmap_Win Universal App - Fatal编程技术网

C# 在UWP应用程序中将遮罩应用于位图图像

C# 在UWP应用程序中将遮罩应用于位图图像,c#,image-processing,bitmap,win-universal-app,C#,Image Processing,Bitmap,Win Universal App,我已经在我的应用程序中打开了一个图像,并使用下面的代码获取图像的像素数据 using (IRandomAccessStream fileStreams = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStreams);

我已经在我的应用程序中打开了一个图像,并使用下面的代码获取图像的像素数据

  using (IRandomAccessStream fileStreams = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStreams);
                BitmapTransform transform = new BitmapTransform()
                {
                    ScaledWidth = Convert.ToUInt32(bitmapImage.PixelWidth),
                    ScaledHeight = Convert.ToUInt32(bitmapImage.PixelHeight)
                };
                PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    transform,
                    ExifOrientationMode.IgnoreExifOrientation,
                    ColorManagementMode.DoNotColorManage
               );

                byte[] sourcePixels = pixelData.DetachPixelData();                
            }
这里我得到了图像中所有像素的数组。此阵列中的像素总数为(宽度*高度*4)。在分析这个数组之后,我知道索引号0、1、2和3包含第一个像素的红、绿、蓝和阿尔法值,索引号4、5、6和7包含图像第二个像素的红、绿、蓝和阿尔法值,依此类推

现在我想将我的3x3过滤器应用于此图像,如何使用此一维阵列?若我有二维图像阵列,我知道怎么做

现在,我有一个想法

  • 在一个2D数组中存储红色像素,在另一个数组中存储绿色像素,依此类推
  • 对每个二维阵列应用过滤器
  • 将所有这些组合起来,再次生成一维数组并返回结果
  • 这是一个好的解决方案吗?
    如果有更好的解决方案,请帮助我。

    如果要在UWP中屏蔽位图图像,则需要使用软件位图作为图像的原始像素数据。 首先,您需要分离图像和掩码数据,并将其转换为BGRA格式的字节数组。 然后,您需要使用不安全代码访问该位置以实现此目的

    创建一个接口

    [ComImport]
    [Guid(“5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D”)]
    [接口类型(ComInterfaceType.InterfaceSiunknown)]
    不安全接口IMemoryBufferByteAccess
    {
    void GetBuffer(输出字节*缓冲区,输出单元容量);
    }

    使用以下代码段更改/编辑软件位图像素

    创建软件位图

    softwareBitmap=新的软件位图(BitmapPixelFormat.Bgra8,100,100,BitmapAlphaMode.Premultiplied)

    使用(BitmapBuffer buffer=softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
    {
    使用(var reference=buffer.CreateReference())
    {
    字节*数据单位字节;
    单位容量;
    ((IMemoryBufferByteAccess)引用).GetBuffer(输出数据字节,输出容量);
    //填写BGRA平面
    BitmapPlaneDescription bufferLayout=buffer.GetPlaneDescription(0);
    对于(int i=0;i
    using (BitmapBuffer buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write))
    {
        using (var reference = buffer.CreateReference())
        {
            byte* dataInBytes;
            uint capacity;
            ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacity);
    
            // Fill-in the BGRA plane
            BitmapPlaneDescription bufferLayout = buffer.GetPlaneDescription(0);
            for (int i = 0; i < bufferLayout.Height; i++)
            {
                for (int j = 0; j < bufferLayout.Width; j++)
                {
    
                    byte value = (byte)((float)j / bufferLayout.Width * 255);
                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 0] = value;
                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 1] = value;
                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 2] = value;
                    dataInBytes[bufferLayout.StartIndex + bufferLayout.Stride * i + 4 * j + 3] = (byte)255;
                }
            }
        }
    }