0

通过使用directshow.net,我可以录制视频,并且通过录制,我正在为此进行文本叠加,我配置了样本采集器,并且在buffercb方法中我正在处理帧,这里是代码..

int ISampleGrabberCB.BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
    {
        Graphics g;
        String s;
        float sLeft;
        float sTop;
        SizeF d;

        g = Graphics.FromImage(bitmapOverlay);
        g.Clear(System.Drawing.Color.Transparent);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        // Prepare to put the specified string on the image
        g.DrawRectangle(System.Drawing.Pens.Transparent, 0, 0, 240 - 1, 176 - 1);
        g.DrawRectangle(System.Drawing.Pens.Transparent, 1, 1, 240 - 3, 176 - 3);

        d = g.MeasureString(m_String + "\n" + DateTime.Now.ToString("G"), fontOverlay);

        sLeft = (240 - d.Width) / 2;
        sTop = (176 - d.Height) / 2;

        g.DrawString(m_String + "\n" + DateTime.Now.ToString("G"), fontOverlay, System.Drawing.Brushes.Black,
            sLeft, sTop, System.Drawing.StringFormat.GenericTypographic);

        // need to flip the bitmap so it's the same orientation as the
        // video buffer
        bitmapOverlay.RotateFlip(RotateFlipType.RotateNoneFlipY);

        // create and copy the video's buffer image to a bitmap
        Bitmap v;
        v = new Bitmap(240, 176, 1056,
            PixelFormat.Format24bppRgb, pBuffer);
        g = Graphics.FromImage(v);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        // draw the overlay bitmap over the video's bitmap
        g.DrawImage(bitmapOverlay, 0, 0, bitmapOverlay.Width, bitmapOverlay.Height);
        // dispose of the various objects
        g.Dispose();
        v.Dispose();
        // Increment frame number.  Done this way, frame are zero indexed.
        m_Count++;
        return 0;
    }

我的问题是,当我启动程序时,它会在预览窗口中显示文本叠加,但是当我打开录制的文件时,文本叠加不会继续..我想我错过了一些框架..在某些框架上叠加是他们的,但它不会继续..它的轻弹。有人可以帮忙吗?

4

1 回答 1

0

好的,我有问题!

在上面的代码中,BufferCB处理当前视频帧的时间太长了。就像让帧A在处理完成帧B进来之前还在中间处理一样。

因此,为了最小化 BufferCB 中的处理,我删除了设置位图图像的位置,我将这行代码放入一个函数中

g = Graphics.FromImage(bitmapOverlay);
    g.Clear(System.Drawing.Color.Transparent);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    // Prepare to put the specified string on the image
    g.DrawRectangle(System.Drawing.Pens.Transparent, 0, 0, 240 - 1, 176 - 1);
    g.DrawRectangle(System.Drawing.Pens.Transparent, 1, 1, 240 - 3, 176 - 3);

    d = g.MeasureString(m_String + "\n" + DateTime.Now.ToString("G"), fontOverlay);

    sLeft = (240 - d.Width) / 2;
    sTop = (176 - d.Height) / 2;

    g.DrawString(m_String + "\n" + DateTime.Now.ToString("G"), fontOverlay, System.Drawing.Brushes.Black,
        sLeft, sTop, System.Drawing.StringFormat.GenericTypographic);

    // need to flip the bitmap so it's the same orientation as the
    // video buffer
    bitmapOverlay.RotateFlip(RotateFlipType.RotateNoneFlipY);

并且在调用 media.run 之前调用此函数。

于 2012-06-29T12:59:42.917 回答