[英]Applying masks to bitmap image in UWP App
I have opened an image in my app and getting the pixels data of image using the following piece of code.我在我的应用程序中打开了一个图像并使用以下代码获取图像的像素数据。
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();
}
Here I have got an array of all the pixels of the image.在这里,我得到了图像所有像素的数组。 The total number of pixels in this array are (width * height * 4) .此数组中的像素总数为(width * height * 4) 。 After analyzing this array I came to know that index numbers 0, 1, 2 and 3 contains the red, green blue and alpha values of the first pixel, index numbers 4, 5, 6 and 7 contains the red, green, blue and alpha values of second pixel of the image and so on.分析这个数组后,我知道索引号 0、1、2 和 3 包含第一个像素的红色、绿色、蓝色和 alpha 值,索引号 4、5、6 和 7 包含红色、绿色、蓝色和 alpha图像的第二个像素的值等等。
Now I want to apply my 3x3 filter to this image, how can I do it with this 1-D array?现在我想将我的 3x3 过滤器应用于这个图像,我该如何使用这个一维数组来做到这一点? I know how to do it if I have 2-D array of the image.如果我有图像的二维数组,我知道该怎么做。
Right now, I have one idea in my mind.现在,我脑子里只有一个想法。
Is it a good solution?这是一个很好的解决方案吗? Help me if there is a better solution to do it.如果有更好的解决方案,请帮助我。
If you want to mask the Bitmapimage in the UWP you need to use a software bitmap for raw pixel data of the image.如果要在 UWP 中屏蔽 Bitmapimage,则需要使用软件位图来获取图像的原始像素数据。 First, you need to separate image and mask data and convert it to a byte array with the format of BGRA.首先需要将image和mask数据分开,转换成BGRA格式的字节数组。 Then after that, you need to access the location using the unsafe code to achieve this.然后,您需要使用不安全代码访问该位置以实现此目的。
Create an interface.创建一个接口。
[ComImport] [Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] unsafe interface IMemoryBufferByteAccess { void GetBuffer(out byte* buffer, out uint capacity); }
Change /Edit software bitmap pixels using the following snippet.使用以下代码段更改/编辑软件位图像素。
create software bitmap .创建软件位图。
softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, 100, 100, BitmapAlphaMode.Premultiplied);
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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.