PictureBox资源发布 [英] PictureBox resources release

查看:99
本文介绍了PictureBox资源发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个屏幕缩放器,以捕获屏幕的一部分并将其缩放.现在,下面的代码可以捕获屏幕并在PictureBox中播放.但是我有一个问题,就是在我打开程序时,我的记忆不断增长.我认为肯定有一些未释放的资源,我也不知道如何释放它.

I want to make a screen zoomer that captures a part of the screen and zoom it. The code below can now capture the screen and play it in a PictureBox. But I have this problem that my memory keeps growing while I open the program. I think there must be some resources that are not released and I don't know How to release it.

我使它像媒体播放器一样,但是它不播放视频,而是播放当前屏幕的一部分.

I'm making it like a media player, but instead of playing videos, it plays a part of the current screen.

public partial class Form1 : Form
{

    PictureBox picBox;
    Bitmap bit;
    Graphics g;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        picBox = pictureBox;
    }

    private void CopyScreen()
    {

        bit = new Bitmap(this.Width, this.Height);
        g = Graphics.FromImage(bit as Image);

        Point upperLeftSource = new Point(
            Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2,
            Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);

        g.CopyFromScreen(upperLeftSource, new Point(0, 0), bit.Size);

        picBox.Image = Image.FromHbitmap(bit.GetHbitmap());

        bit.Dispose();
        g.Dispose();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        CopyScreen();
    }

推荐答案

问题在于您使用了GetHbitmap,并且当您分配新的Image >到PictureBox.

The issue is with your use of GetHbitmap, and the fact that you aren't disposing of the previous Image when you assign a new Image to the PictureBox.

https://msdn.microsoft. com/en-us/library/1dz311e4(v = vs.110).aspx 状态:

您负责调用GDI DeleteObject方法以释放 GDI位图对象使用的内存.

You are responsible for calling the GDI DeleteObject method to free the memory used by the GDI bitmap object.

(您不这样做)

请考虑更改代码以避免进行GetHbitmap调用(以及之前的ImageDispose)调用:

Consider changing the code to avoid the need for the GetHbitmap call (and to Dispose the previous Image):

private void CopyScreen()
{
    bit = new Bitmap(this.Width, this.Height);
    g = Graphics.FromImage(bit);

    Point upperLeftSource = new Point(
        Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2,
        Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);

    g.CopyFromScreen(upperLeftSource, new Point(0, 0), bit.Size);

    var oldImage = picBox.Image;
    picBox.Image = bit;
    oldImage?.Dispose();

    g.Dispose();
}

要进一步简化,请删除您在类顶部声明的字段,然后使用:

To simplify it further, remove the fields you have declared at the top of the class, and just use:

private void CopyScreen()
{
    var picBox = pictureBox;
    var bit = new Bitmap(this.Width, this.Height);

    using (var g = Graphics.FromImage(bit))
    {
        var upperLeftSource = new Point(
            Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2,
            Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2);

        g.CopyFromScreen(upperLeftSource, new Point(0, 0), bit.Size);

        var oldImage = picBox.Image;
        picBox.Image = bit;
        oldImage?.Dispose();
    }
}

这篇关于PictureBox资源发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆