Question

I am trying to convert every frame I get from my sample grabber into a bitmap however it does not seem to work.

I am using the SampleCB as follows:

int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample sample)
    {
        try
        {
            int lengthOfFrame = sample.GetActualDataLength();
            IntPtr buffer;
            if (sample.GetPointer(out buffer) == 0 && lengthOfFrame > 0)
            {
                Bitmap bitmapOfFrame = new Bitmap(width, height, capturePitch, PixelFormat.Format24bppRgb, buffer);
                Graphics g = Graphics.FromImage(bitmapOfFrame);
                Pen framePen = new Pen(Color.Black);
                g.DrawLine(framePen, 30, 30, 50, 50);
                g.Flush();
            }
        CopyMemory(imageBuffer, buffer, lengthOfFrame);           
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

        Marshal.ReleaseComObject(sample);


        return 0;
    }

I am drawing a small graphic on it as a tester and it does not seem to work. From what I believe this should be adding a small line to each frame therefore updating my preview with the line.

I can give additional code if needed (e.g How i set up my graph and connect my ISampleGrabber)

Edited with what i think Dee Mon means:

int ISampleGrabberCB.SampleCB(double SampleTime, IMediaSample sample)
{
    try
    {        

        int lengthOfFrame = sample.GetActualDataLength();
        IntPtr buffer;
        BitmapData bitmapData = new BitmapData();
        if (sample.GetPointer(out buffer) == 0 && lengthOfFrame > 0)
        {                    
            Bitmap bitmapOfFrame = new Bitmap(width, height, capturePitch, PixelFormat.Format24bppRgb, buffer);                    
            Graphics g = Graphics.FromImage(bitmapOfFrame);
            Pen framePen = new Pen(Color.Black);
            g.DrawLine(framePen, 30, 30, 50, 50);
            g.Flush();
            Rectangle rect = new Rectangle(0, 0, bitmapOfFrame.Width, bitmapOfFrame.Height);
            bitmapData = bitmapOfFrame.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            IntPtr bitmapPointer = bitmapData.Scan0;


            CopyMemory(bitmapPointer, buffer, lengthOfFrame); 
            BitmapOfFrame.UnlockData(bitmapData);
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

    Marshal.ReleaseComObject(sample);


    return 0;
}
Was it helpful?

Solution

When you create a Bitmap it copies data to its own internal buffer and all the drawing goes in that buffer, not in yours. Use Bitmap.LockBits and BitmapData class to get its contents after you draw your stuff in the bitmap.

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