Question

I am using the excellent VLC wrapper created by Roman Ginzburg re: nVLC

This part of his code returns a bitmap object. In myh calling code I then convert it to a byte array. I sthere a way to directly convert the memory pointer to a byte array with converting to a bitmap object.

this is his code:

    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));
    }

What I would like is another function like:

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

I am lookinga s I type but still cannot find anything or even if it possible to do so...

Thanks

Was it helpful?

Solution

Use Marshal.Copy. Like this:

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

I'm not quite sure where you are storing the size of the buffer, but you must know that.

An aside. Why do you write new IntPtr(m_pBuffer) in GetBitmap rather than plain m_pBuffer?

I also wonder why you feel the need to use unsafe code here. Is it really needed?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top