我在一个应用程序中工作,我需要从图像获取像素数组并使用pixels数组编辑图像。
我正在使用下一个代码从表示图像的StorageFile对象获取像素数组:
public static async Task<byte[]> GetPixelsArrayFromStorageFileAsync(
IRandomAccessStreamReference file)
{
using (IRandomAccessStream stream = await file.OpenReadAsync())
{
using (var reader = new DataReader(stream.GetInputStreamAt(0)))
{
await reader.LoadAsync((uint)stream.Size);
var pixelByte = new byte[stream.Size];
reader.ReadBytes(pixelByte);
return pixelByte;
}
}
}
现在,我的问题是:
- 为什么要加载6000 x 4000像素的图像,而数组只有8,941,799,这实际上是磁盘上图像的大小?
- 如何访问像素的RGBA通道?
您的文件具有位图的压缩版本,因此您需要先对其进行解码。我建议您将其加载到中,WriteableBitmap
因为无论如何您都需要显示它,然后访问PixelBuffer
位图的属性以获取实际像素。您可以执行以下操作:
var writeableBitmap = new WriteableBitmap(1, 1);
await writeableBitmap.SetSourceAsync(yourFileStream);
var pixelStream = writeableBitmap.PixelBuffer.AsStream();
var bytes = new byte[pixelStream.Length];
pixelStream.Seek(0, SeekOrigin.Begin);
pixelStream.Read(bytes, 0, Bytes.Length);
// Update the bytes here. I think they follow the BGRA pixel format.
pixelStream.Seek(0, SeekOrigin.Begin);
pixelStream.Write(bytes, 0, bytes.Length);
writeableBitmap.Invalidate();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句