制作屏幕截图的速度非常快,而且内存不足



我正在尝试快速制作屏幕截图。

所以,我在我的主类中使用这个代码

[STAThread]
static void Main(string[] args)
{
    int x = 1;
    int screenshotsAmount = 0;
    List<Bitmap> screenshots = new List<Bitmap>();
    while (x == 1)
    {
        screenshots.Add(FullsizeScreenshot.makeScreenshot());
        Clipboard.SetImage(screenshots[screenshotsAmount]);
        Console.WriteLine("Screenshot " + screenshotsAmount + " has been made and added to the Bitmap list!");
        screenshotsAmount++;
    }
}

我在我的.dll中有一个方法,可以创建屏幕截图

// Class for making standard screenshots
public struct FullsizeScreenshot
{
    // Making fullscreen screenshot
    public static Bitmap makeScreenshot() 
    {
        Bitmap screenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            Graphics gfxScreenshot = Graphics.FromImage(screenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            gfxScreenshot.Dispose();
            return screenshot;
    }
}

一切正常,但当屏幕截图的数量超过109时,我的程序就会崩溃,出现System.ArgumentException

System.Drawing.dll中类型为"System.ArgumentException"的未处理异常附加信息:参数无效

这一行抛出:gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PermaryScreen.Bounds.Y,0,0,Screen_PrimaryScren.Bounds.Size,CopyPixelOperation.SourceCopy)或者这样:位图屏幕截图=新位图(Screen.PrimaryScreen.Bounds.Width、Screen.PprimaryScreen.Bunds.Height、PixelFormat.Format32ppArgb)

我试着使用(位图截图…)和.Dispose(),但它不正确,因为这些类(位图和图形)是类,它们只是创建链接,而不是复制。因此,当我在makeScreenshot()中处理位图时,它破坏了列表中的位图。

那么,我该怎么办?也许我应该复印一份,但我不知道怎么做。

假设您的显示器为1920x1080,即2073600像素,PixelFormat.Format32bppArgb中每个像素有4个字节,即8294400字节,约为8MB。109个图像将为872MB。令人惊讶的是,它在那里崩溃了,但你明白了,这是太多的记忆。

如果你想制作一个动画gif,想想它会有多大,全屏?我希望你没有打算,这对gif来说不现实。截图后,立即将其缩小到目标分辨率,以减少内存占用。

最新更新