c# - 从 UWP 中的 BitmapImage 或流获取 IntPtr 值

标签 c# .net pointers image-processing uwp

我正在 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/

相关文章:

mysql - 将数据绑定(bind)到 GridView

c - 为什么我不能将赋值分解为两个不同的语句?

c# - DataContractJsonSerializer 跳过具有空值的节点

c# - 将两个关键字列表列与另一列匹配以进行匹配

C# Socket.Send() : does it send all data or not?

c# - 将以下 C# 示例转换为 VB.NET

c# - 如何在单个整数中编码两个数字?

c# - 引用大小结构的原子分配

c++ - 删除后我应该分配我的指针 0 吗?

arrays - 下标值既不是数组也不是指针错误