保存屏幕截图图像时出现内存不足异常,即使每次处理时也是如此

本文关键字:处理 图像 屏幕截图 内存不足 保存 异常 | 更新日期: 2023-09-27 18:00:18

我有一个保存桌面图像屏幕截图的类。

#region Class Imports 
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
#endregion
namespace Manager
{
    public class ScreenShot
    {
    #region Global Variables
    private Bitmap _screenShot;
    protected static IntPtr newBMP;
    #endregion
    #region Constants
    public const int SRCCOPY = 13369376;
    public const int SCREEN_X = 0;
    public const int SCREEN_Y = 1;
    #endregion
    #region Class Properties
    [Description("Gets the screenshot image")]
    public Bitmap ScreenImage
    {
        get { return _screenShot; }
    }
    #endregion
    #region Constructor
    [Description("Empty constructor, instantiating _screenShot to nothing")]
    public ScreenShot()
    {
        _screenShot = null;
    }              
    #endregion
    #region Methods
    [Description("Creates an image of the current desktop")]
    public Bitmap GetScreen()
    {
        int xLoc;
        int yLoc;
        IntPtr dsk;
        IntPtr mem;
        Bitmap currentView;
        //get the handle of the desktop DC
        dsk = Win32API.GetDC(Win32API.GetDesktopWindow());
        //create memory DC
        mem = Win32API.CreateCompatibleDC(dsk);
        //get the X coordinates of the screen
        xLoc = Win32API.GetSystemMetrics(SCREEN_X);
        //get the Y coordinates of screen.
        yLoc = Win32API.GetSystemMetrics(SCREEN_Y);
        //create a compatible image the size of the desktop
        newBMP = Win32API.CreateCompatibleBitmap(dsk, xLoc, yLoc);
        //check against IntPtr (cant check IntPtr values against a null value)
        if (newBMP != IntPtr.Zero)
        {
            //select the image in memory
            IntPtr oldBmp = (IntPtr)Win32API.SelectObject(mem, newBMP);
            //copy the new bitmap into memory
            Win32API.BitBlt(mem, 0, 0, xLoc, yLoc, dsk, 0, 0, SRCCOPY);
            //select the old bitmap into memory
            Win32API.SelectObject(mem, oldBmp);
            //delete the memoryDC since we're through with it
            Win32API.DeleteDC(mem);
            //release dskTopDC to free up the resources
            Win32API.ReleaseDC(Win32API.GetDesktopWindow(), dsk);
            //create out BitMap
            currentView = Image.FromHbitmap(newBMP);
            //return the image
            return currentView;
        }
        else  //null value returned
        {
            return null;
        }    
    }
    #endregion
    public void GetScreenShot(string folder, string name)
    {
        _screenShot = new Bitmap(GetScreen());
        string ingName = folder + name + Elgato_Video_Capture.counter.ToString("D6") + ".bmp";
        _screenShot.Save(ingName);
        _screenShot.Dispose();
    }
    }
}

在表格1中使用计时器:

private void button1_Click(object sender, EventArgs e)
{
    timer1.Start();
}

在勾选事件中:

ScreenShot shot = new ScreenShot();
public static int counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
    counter++;
    shot.GetScreenShot(@"e:'screenshots'", "screenshot");
    if (counter == 1200)
    {
        timer1.Stop();
        ScreenShotsPlayer ssp = new ScreenShotsPlayer();
        ssp.Show();
    }
}

异常出现在行底部的新类中:

_screenShot = new Bitmap(GetScreen());

并且它一直发生在保存图像编号截图000147.bmp 之后

System.OutOfMemoryException occurred
  HResult=-2147024882
  Message=Out of memory.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
       at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
       at System.Drawing.Bitmap..ctor(Image original, Int32 width, Int32 height)
       at System.Drawing.Bitmap..ctor(Image original)
       at Youtube_Manager.ScreenShot.GetScreenShot(String folder, String name) in d:'C-Sharp'Manager'Manager'Manager'ScreenShot.cs:line 105
  InnerException: 

第105行为:_screenShot = new Bitmap(GetScreen())

每次保存后,我都会处理_screenShot变量。也许表1中的计时器太快了?计时器间隔设置为100ms。

保存屏幕截图图像时出现内存不足异常,即使每次处理时也是如此

Image.FromHbitmap(newBMP)基于源进行复制;因此原始CCD_ 3"丢失在非托管内存中",因为它从未被释放。

FromHbitmap方法生成GDI位图的副本;因此您可以在创建新图像后立即释放传入的GDI位图(使用GDI DeleteObject方法)