Question

I've been working with the DxLogo project from the DirectShowNET samples. This will add an image to video but when moved it distorts the images color and it also doesn't handle transparent backgrounds. So in order for it to handle those factors...

Some how I need to convert the frame to a bitmap:

Bitmap v = new Bitmap(m_videoWidth, m_videoHeight, m_stride, PixelFormat.Format32bppArgb, pBuffer);

Then add the watermark to the new bitmap:

Graphics g = Graphics.FromImage(v);
g.DrawImage(m_Bitmap, 0, 0, m_Bitmap.Width, m_Bitmap.Height);
v = new Bitmap(v.Width, v.Height, g);

Then have the BufferCB take the new bitmap as the frame, and that's where I'm stuck. How would you do that?

Original DxLogo BufferCB method:

int ISampleGrabberCB.BufferCB( double SampleTime, IntPtr pBuffer, int BufferLen )
{
    // Avoid the possibility that someone is calling SetLogo() at this instant
    lock (this)
    {            
        if (m_bmdLogo != null)
        {                    
            for (int x = 0; x < m_bmdLogo.Height; x++)
            {                        
                CopyMemory(ipDest, ipSource, (uint)m_bmdLogo.Stride);
                ipSource = (IntPtr)(ipSource.ToInt32() + m_bmdLogo.Stride);
                ipDest = (IntPtr)(ipDest.ToInt32() + m_stride);
            }
        }
    }

    return 0;
}

Thanks!

Was it helpful?

Solution

Found the issue in case anyone else runs into the problem. I was using the wrong PixelFormat when creating the bitmat from the pBuffer. Instead of PixelFormat.Format32bppArgb use PixelFormat.Format24bppRgb.

Here's my working BufferCB for the DxLogo sample:

int ISampleGrabberCB.BufferCB( double SampleTime, IntPtr pBuffer, int BufferLen )
{
    // Avoid the possibility that someone is calling SetLogo() at this instant
    lock (this)
    {
        if (m_Bitmap != null)
        {                        
            Bitmap v;
            v = new Bitmap(m_videoWidth, m_videoHeight, m_stride, PixelFormat.Format24bppRgb, pBuffer);

            Graphics g = Graphics.FromImage(v);
            g.DrawImage(m_Bitmap, 100, 100, m_Bitmap.Width, m_Bitmap.Height);                
        }
    }

This solves the issue where the images colors would distort if repositioned and transparent backgrounds now work.

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