我正在尝试将从样本采集器获得的每一帧转换为位图,但它似乎不起作用。
我正在使用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)
用我认为 Dee Mon 的意思编辑:
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;
}