2013-11-03 73 views
1

我想将我从示例采集器中获得的每个帧转换为位图,但它似乎不起作用。ISampleGrabberCB ::示例不工作

我用SampleCB如下:

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

我画上有一个小的图形作为测试人员,它似乎并没有工作。从我相信这应该是添加一个小行到每个帧,因此更新我的预览线。

如果需要的话我可以给额外的代码(如我如何设置我的图形和连接我的ISampleGrabber)

编辑与我想迪周一表示:

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

回答

2

当你创建一个位图它将数据复制到其自己的内部缓冲区,并且所有绘图都进入该缓冲区,而不是在您的缓冲区中。在位图中绘制东西后,使用Bitmap.LockBits和BitmapData类来获取其内容。

+0

我已经添加了我对原始帖子中的含义的解释。你是这个意思吗? – legohead

+0

那么,你已经有了bitmapData,但是你并没有使用它。至少在这个片段。你想要更新的图片保存在一个bmp文件中,或只是传递给一个渲染器? –

+0

基本上我想处理每一个新的框架。我想使用位图来完成这个任务,因为外部库需要处理每个图像,我还想为每个帧绘制一个十字准线,所以我认为将图形添加到位图会比其他方法更容易。 – legohead