C# 位图帧限制

C# 位图帧限制,c#,memory,bitmap,overloading,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, S

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

我的代码如下:

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);
或者

这说明我的一个参数无效

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

编辑 --感谢您通知我有关
使用
语句的信息,这非常有用,并修复了内存过载问题

我应该在前面指出,我正在使用一个名为的库,现在我在尝试向视频编写器添加帧时出错

错误为:'System.ArgumentException'类型的未处理异常发生在forge.Video.FFMPEG.dll中

附加信息:提供的位图必须是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";
        }
    }

再次感谢

你能发布准确的错误消息和它可能存在的任何内部异常吗?你是否在某处处置位图?这可能是内存不足的问题。请注意,根据屏幕分辨率和位深度,每个位图可以分配大约2兆字节的内存。使用
.Dispose()
时,我看不到任何
。您确实需要执行其中一项操作,因为您的许多对象都在使用非托管资源;这包括您不断创建的
位图
图形
对象。。!我已经编辑了你的标题。请参阅“”,其中的共识是“不,他们不应该”。
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";
        }
    }