使用SharpAvi记录屏幕活动

时间:2018-06-08 10:35:53

标签: c# wpf video-capture

我在使用SharpAvi录制屏幕活动时出现问题,这是我的代码:

    SharpAvi.Output.AviWriter aviW = new SharpAvi.Output.AviWriter(videoRecorderFileLocation);
    SharpAvi.Output.IAviVideoStream streamAVI = aviW.AddVideoStream(500, 300, SharpAvi.BitsPerPixel.Bpp32);
    streamAVI.Codec = SharpAvi.KnownFourCCs.Codecs.MicrosoftMpeg4V3;

在计时器上,每40毫秒我会记录一次这样的图像:

        System.Drawing.Bitmap image = new System.Drawing.Bitmap(500, 300, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.CopyFromScreen(0, 0, 0, 0, new System.Drawing.Size(500, 300), System.Drawing.CopyPixelOperation.SourceCopy);

        using (MemoryStream ms = new MemoryStream())
        {
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            streamAVI.WriteFrame(true, // is key frame? (many codecs use concept of key frames, for others - all frames are keys)
                  ms.ToArray(), // array with frame data
                  0, // starting index in the array
                  ms.ToArray().Length // length of the data
            );
        }

        videoRecorderTimer.Start();

我用

关闭视频
        videoRecorderTimer.Stop();
        aviW.Close();

视频文件已正确创建,但当我使用VLC阅读时,它没有图像(或有黑色图像)。

感谢您的帮助。

0 个答案:

没有答案