文字叠加问题?

时间:2012-06-22 12:59:15

标签: directshow.net

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

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

1 个答案:

答案 0 :(得分: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之前调用此函数。