【问题标题】:ISampleGrabberCB::Sample not workingISampleGrabberCB::样本不工作
【发布时间】:2013-11-03 22:13:09
【问题描述】:

我正在尝试将从样本采集器获取的每一帧转换为位图,但它似乎不起作用。

我使用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;
}

【问题讨论】:

    标签: c# image-processing bitmap directshow directshow.net


    【解决方案1】:

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

    【讨论】:

    • 我已经添加了我对您在原始帖子中的意思的解释。这是你的意思吗?
    • 嗯,你有 bitmapData 但是你不使用它。至少在这个 sn-p 中。您希望将更新后的图片保存在 bmp 文件中还是直接将流传递给渲染器?
    • 基本上我想处理每个新帧。我想使用位图来执行此操作,因为将处理每个图像的外部库需要位图,我还想为每个帧绘制一个十字准线,所以我认为将图形添加到位图会比其他方法更容易。
    • 我已经用我认为应该对位图数据执行的操作更改了 OP 以实现上述结果,对吗?
    • 你越来越近了。但我认为你的 CopyMemory 参数错误。第一个应该是目的地,第二个应该是来源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多