位图帧限制

时间:2015-04-28 17:30:05

标签: c# memory bitmap overloading

我正在开发一个屏幕录制程序,我遇到了认为是内存问题。

我的代码如下:

public void RecordTimer_Tick(object sender, EventArgs e)
{
    screenBounds = Screen.PrimaryScreen.Bounds;

    lastFrame = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

    Graphics graphics = Graphics.FromImage(lastFrame);
    graphics.CopyFromScreen(SystemInformation.VirtualScreen.X,
                    SystemInformation.VirtualScreen.Y,
                    0,
                    0,
                    SystemInformation.VirtualScreen.Size,
                    CopyPixelOperation.SourceCopy);

    recordedFrames.Add(lastFrame);
}

在记录了大约100帧后,我发现错误

lastFrame = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

Graphics graphics = Graphics.FromImage(lastFrame);
graphics.CopyFromScreen(SystemInformation.VirtualScreen.X,
                SystemInformation.VirtualScreen.Y,
                0,
                0,
                SystemInformation.VirtualScreen.Size,
                CopyPixelOperation.SourceCopy);

这告诉我其中一个参数无效。

我注意到当我将位图的像素格式从32更改为24时,它会在崩溃前达到约120帧。

修改的   - 感谢您告诉我有关using语句的信息,这非常有用并解决了内存过载问题。

我之前应该注意到我正在使用名为AforgeNet的库,现在我在尝试向视频编写器添加帧时遇到错误。

错误是:`AForge.Video.FFMPEG.dll中发生了'System.ArgumentException'类型的未处理异常

附加信息:提供的位图必须是24或32 bpp彩色图像或8 bpp灰度图像。

这是我记录帧的代码:

public void RecordTimer_Tick(object sender, EventArgs e)
    {
        screenBounds = Screen.PrimaryScreen.Bounds;

        // lastFrame = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);

        using (Bitmap lastFrame = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb))
        {
            using (Graphics graphics = Graphics.FromImage(lastFrame))
            {
                graphics.CopyFromScreen(SystemInformation.VirtualScreen.X,
                                SystemInformation.VirtualScreen.Y,
                                0,
                                0,
                                SystemInformation.VirtualScreen.Size,
                                CopyPixelOperation.SourceCopy);
            }

            recordedFrames.Add(lastFrame);
        }
    }

这是我的代码,用于向视频编写器添加帧,在之后发生我记录所有帧:

public void Button_StopRecording_OnClick(object sender, EventArgs e)
    {
        if (recording == true)
        {
            recording = false;
            recordTimer.Stop();

            // Write all of the recorded frames to the video.
            foreach (Bitmap frame in recordedFrames)
            {
                videoWriter.WriteVideoFrame(frame);
            }

            // Then, close the video writer.
            videoWriter.Close();

            // Empty the list of recorded frames.
            recordedFrames.Clear();

            Text = "Screen Recorder";
        }
    }

再次感谢!

0 个答案:

没有答案