我正在 WinForm 中进行图像处理,当我有 Bitmap 和 BitmapData 时它工作得很好,我可以轻松地从中获取 IntPtr。但在 UWP 中,我无法从他们那里获取 IntPtr。那么我们有什么办法可以做到这一点吗?
更新:如果我们无法获取 IntPtr 值,我们可以获取该图像的指针地址吗? WinForm 中是这样的:
byte* src = (byte*) BitmapData.Scan0.ToPointer( );
最佳答案
您可以通过 BitmapDecoder 和 PixelDataProvider 从文件流中获取 pxiel 数据:
Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/StoreLogo.png")).OpenReadAsync();
Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
byte[] buffer = pixelData.DetachPixelData();
然后你可以通过不安全的代码从字节数组中获取Intptr
unsafe
{
fixed (byte* p = buffer)
{
IntPtr ptr = (IntPtr)p;
// do you stuff here
}
}
如果编译不安全代码,您需要在项目的构建属性中启用允许不安全代码选项。
关于c# - 从 UWP 中的 BitmapImage 或流获取 IntPtr 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42522747/