0

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

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks for replying. All this code is someone else's (I got it from CodeProject re: link). I just found Marshal.Copy the same time you posted but as it is the right answer you get a tick :). If I 'hacked' this library to remove unsafe I assume it will still work then? Is there a trade-off for using unsafe code in terms of speed/efficiency. I will use m_pBuffer as you suggested though. I await to be educated . Really appreciate ur time.
There can be times where unsafe would be faster. It all depends. Doesn't look like that's the case here. People often use unsafe because they don't know how to use the Marshal class.
HI, Well this is a real-time application for video-streaming so response times is vital for me. Thank you you have been very helpful. I do have another question from this solution but I have posted it as a separate question stackoverflow.com/questions/20116525/… . Pls feel free to look at it :)
You can avoid the unsafe in the code in the Q by making the first parameter of the function ref PixelData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.