C#内存在Bitmap中泄漏

时间:2013-02-19 12:48:06

标签: c# image bitmap

我的应用程序中存在内存泄漏问题。如果我查看任务管理器,每次触发此过程时,RAM内存增加+ - 300 MB ..

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

有人可以帮助我解决他的泄漏问题吗?如果我使用:

bmp1.Dispose();

我在此行的“Program.cs”中遇到异常:Application.Run(new Form1()); 在此之后,应用程序停止运行...

屏幕应用: enter image description here

4 个答案:

答案 0 :(得分:9)

更新:您没有内存泄漏本身,您只需等待垃圾收集器释放资源。

如果你想要制作垃圾收集器collect,你可以这样做:

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

为什么需要处理位图?如果您的PictureBox正在使用它,那么您需要位图。如果你要更改它,也许你应该将旧的位图换成新的位图并处理旧的位图:

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(); // the first time it'll be null

答案 1 :(得分:3)

我认为您应该只处理不再需要的图像。您仍需要创建bmp1,只需将其设置为pictureBox2.Image字段的内容即可。尝试以下几点:

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();

免责声明:我对C#没有经验,所以我可能错了......

答案 2 :(得分:0)

您应该使用外部gdi32.dll以避免位图内存泄漏

[System.Runtime.InteropServices.DllImport("gdi32.dll")] 
public static extern bool DeleteObject(IntPtr hObject);
...
//delete bitmap handle
DeleteObject((IntPtr)hBitmap);

答案 3 :(得分:-1)

    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;

    }