C# PictureBox资源发布

C# PictureBox资源发布,c#,picturebox,C#,Picturebox,我想制作一个屏幕缩放器,捕捉屏幕的一部分并进行缩放。下面的代码现在可以捕获屏幕并在PictureBox中播放。但是我有一个问题,就是当我打开程序的时候,我的记忆一直在增长。我想一定有一些资源没有发布,我不知道如何发布 我把它做成一个媒体播放器,但它不是播放视频,而是在当前屏幕上播放一部分 public partial class Form1 : Form { PictureBox picBox; Bitmap bit; Graphics g; public F

我想制作一个屏幕缩放器,捕捉屏幕的一部分并进行缩放。下面的代码现在可以捕获屏幕并在PictureBox中播放。但是我有一个问题,就是当我打开程序的时候,我的记忆一直在增长。我想一定有一些资源没有发布,我不知道如何发布

我把它做成一个媒体播放器,但它不是播放视频,而是在当前屏幕上播放一部分

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
的使用,以及在将新的
图像
分配给
PictureBox
时没有处理以前的
图像

国家:

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

(你没有这样做)

考虑更改代码,以避免调用
GetHbitmap
(以及
Dispose
上一张
Image
):

为了进一步简化,请删除在类顶部声明的字段,只需使用:

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

问题在于您对
GetHbitmap
的使用,以及在将新的
图像
分配给
PictureBox
时没有处理以前的
图像

国家:

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

(你没有这样做)

考虑更改代码,以避免调用
GetHbitmap
(以及
Dispose
上一张
Image
):

为了进一步简化,请删除在类顶部声明的字段,只需使用:

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

看看@mjwills,它确实有效。。。记忆还保留着growing@mjwills我将计时器的间隔设置为20,因此内存增长非常快,比如每秒10MB看看@mjwills,它确实工作了。。。记忆还保留着growing@mjwills我将计时器的间隔设置为20,因此内存增长非常快,比如每秒10MB。非常感谢您抽出时间来帮助我!你们和所有贡献自己的人使Stack Overflow成为程序员提问的最佳场所!我敬佩你,我会尽我所能帮助别人。对不起,我的英语很差。非常感谢你花时间帮助我!你们和所有贡献自己的人使Stack Overflow成为程序员提问的最佳场所!我敬佩你,我会尽我所能帮助别人。对不起,我的英语很差。