簡體   English   中英

將內存指針轉換為字節數組

[英]Convert memory pointer to byte array

我使用的是Roman Ginzburg re: nVLC創建的出色的VLC包裝器

他的代碼的這一部分返回一個位圖對象。 然后在myh調用代碼中,將其轉換為字節數組。 我使用了一種方法,可以將內存指針直接轉換為字節數組,然后再轉換為位圖對象。

這是他的代碼:

    unsafe void OnpDisplay(void* opaque, void* picture)
    {
        lock (m_lock)
        {
            PixelData* px = (PixelData*)opaque;
            MemoryHeap.CopyMemory(m_pBuffer, px->pPixelData, px->size);

            m_frameRate++;
            if (m_callback != null)
            {
                using (Bitmap frame = GetBitmap())
                {
                    m_callback(frame);
                }
            }
        }
    }

    private Bitmap GetBitmap()
    {
        return new Bitmap(m_format.Width, m_format.Height, m_format.Pitch, m_format.PixelFormat, new IntPtr(m_pBuffer));
    }

我想要的是另一個功能,例如:

private byte[] GetBytes()
{
 //not sure what to put here...
}

我正在尋找輸入內容,但仍然找不到任何東西,即使可能也無法找到...

謝謝

使用Marshal.Copy。 像這樣:

private byte[] GetBytes() {
    byte[] bytes = new byte[size];
    Marshal.Copy(m_pBuffer, bytes, 0, size);
    return bytes;
}

我不太確定要在哪里存儲緩沖區的大小,但是您必須知道這一點。

放在一邊。 為什么在GetBitmap中而不​​是普通的m_pBuffer中編寫新的IntPtr(m_pBuffer)?

我也想知道為什么您覺得需要在這里使用不安全的代碼。 真的需要嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM