简体   繁体   English

C#位图中的内存泄漏

[英]C# Memory leak in Bitmap

I got a memory leak in my application in this lines.. If i take a look in the task manager, every time when this process is triggered, the RAM memory is increasing by +- 300 MB..我的应用程序在这一行中出现了内存泄漏.. 如果我查看任务管理器,每次触发此进程时,RAM 内存都会增加 +- 300 MB ..

Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
pictureBox2.Image = bmp1;

Can someone help me with his leak?有人可以帮我解决他的泄密问题吗? If I use:如果我使用:

bmp1.Dispose();

I get an exception in "Program.cs" at this line: Application.Run(new Form1());我在“Program.cs”这一行得到一个异常: Application.Run(new Form1()); And after this, the application is stopped running...在此之后,应用程序停止运行......

Screen application:屏幕应用:在此处输入图片说明

Update: You don't have a memory leak per se , you just have to wait for the Garbage Collector to free up the resources.更新:本身没有内存泄漏,您只需要等待垃圾收集器释放资源。

If you do want to make the garbage collector collect though, you can do this:如果你确实想让垃圾收集器collect ,你可以这样做:

System.GC.Collect();
System.GC.WaitForPendingFinalizers();

Why do you need to dispose of the bitmap?为什么需要处理位图? If your PictureBox is using it, then you need the bitmap.如果您的 PictureBox 正在使用它,那么您需要位图。 If you're changing it a lot, maybe you should switch out the old bitmap for a new one and dispose of the old one:如果您要对其进行大量更改,也许您应该将旧位图切换为新位图并处理旧位图:

Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
Image img = pictureBox1.Image;
pictureBox1.Image = bmp1;
if (img != null) img.Dispose(); // this may be null on the first iteration

I assume you should dispose only the image you don't need anymore .我假设您应该只处理不再需要的图像。 You still need the bmp1 created, you just set it to be the content of the pictureBox2.Image field .您仍然需要创建bmp1 ,您只需将其设置为pictureBox2.Image字段的内容。 Try something along these lines:尝试按照以下方式进行操作:

Bitmap bmp1 = new Bitmap(2480, 3508);
panel1.DrawToBitmap(bmp1, new Rectangle(0, 0, 2480, 3508));
Bitmap bmp2 = (Bitmap)pictureBox2.Image;
pictureBox2.Image = bmp1;
bmp2.Dispose();

Disclaimer: I'm not experienced with C#, so I might be wrong...免责声明:我对 C# 没有经验,所以我可能是错的......

You should use external gdi32.dll for avoiding bitmap memory leaks您应该使用外部 gdi32.dll 来避免位图内存泄漏

[System.Runtime.InteropServices.DllImport("gdi32.dll")] 
public static extern bool DeleteObject(IntPtr hObject);
//your bitmap usage code here
...
//delete bitmap handle when you don't need the bitmap anymore
DeleteObject((IntPtr)hBitmap);
    Bitmap copy_Screen()
    {
        try
        {
            var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            try
            {
                gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            }
            catch (Exception) { }
            return bmpScreenshot;
        }
        catch (Exception) { }
        return new Bitmap(10, 10, PixelFormat.Format32bppArgb);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        //Start Stop timer
        if (timer1.Enabled == false) { timer1.Enabled = true; } else { timer1.Enabled = false; }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        //memory leak solve
        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();

        Bitmap BM = copy_Screen();
        if (pictureBox1.Image != null)
        {
            pictureBox1.Image.Dispose();
        }
        pictureBox1.Image = BM;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM