【问题标题】:Handle byte* pointer and convert it to something else处理 byte* 指针并将其转换为其他内容
【发布时间】:2018-11-13 03:31:59
【问题描述】:

我有一个可能很愚蠢的问题,但我不知道如何自己解决。

我从 SoftwareBitmap 的像素数据中获得了一个 byte* 指针,然后我编辑了一些像素数据……现在我不知道如何使用那个 byte* 指针。

如何将其转换为 IBuffer,例如,以便使用它来创建新的 SoftwareBitmap? 有没有更简单的方法来创建新的位图?

这是代码,顺便说一句,甚至认为它不应该有任何帮助:

  1. DLL 导入

    [ComImport]
    [Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    unsafe interface IMemoryBufferByteAccess
    {
        void GetBuffer(out byte* buffer, out uint capacity);
    }
    
  2. 主要功能

    private unsafe SoftwareBitmap PixelateImage(SoftwareBitmap bitmap, Boolean AlphaEnabled)
    {
        using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.Read))
        {
            using (var reference = buffer.CreateReference())
            {
                ((IMemoryBufferByteAccess)reference).GetBuffer(out byte* data, out uint capacity);
    
                // Doing things with data[int index] bytes…
    
                SoftwareBitmap bmp = new SoftwareBitmap(bitmap.BitmapPixelFormat, bitmap.PixelWidth, bitmap.PixelHeight, bitmap.BitmapAlphaMode);
    
                // How to use byte* data else?
                // bmp.CopyFromBuffer(IBuffer)  <=== How to get an IBuffer from byte*?
    
                return bmp;
            }
        }
    }
    

谢谢大家。

【问题讨论】:

    标签: c# arrays image pointers uwp


    【解决方案1】:

    字节指针在 .NET 世界中可以看作是一个不安全的字节数组,所以首先我们需要做这个编组:

    var safearray = new byte[capacity];
    Marshal.Copy((IntPtr)data, safearray, 0, capacity);
    

    (另一种选择是不安全的强制转换以避免复制,如果性能是一个问题,即如果您正在处理许多和/或大型位图)。

    那么我们可以简单的写成:

    IBuffer safebuffer = safearray.AsBuffer();
    

    这是 System.Runtime.InteropServices.WindowsRuntime 命名空间的扩展。

    【讨论】:

    • 是的,性能非常重要,因为我需要处理甚至 20 mpx 的图像......那么有什么替代方案?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2015-04-24
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多