Windows存储-像素数组
本文关键字:数组 -像素 存储 Windows | 更新日期: 2023-09-27 18:15:47
我正在一个应用程序中工作,我需要从图像中获得像素数组,并使用像素数组编辑图像。
我使用下一个代码从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();
你可以检查这里和这里的扩展方法,看看如何使用像素