正确的方式处置图像/位图和图片框 [英] Right way to dispose Image/Bitmap and PictureBox

查看:122
本文介绍了正确的方式处置图像/位图和图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个的Windows Mobile 6(WF中/ C#)应用程序。只有一个表格,在表格上只有一个图片对象。它我画所需的全部控制,我想做的事情。

I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls or whatever I want.

有两件事情我做。绘制自定义形状和.png文件加载位图。

There are two things I am doing. Drawing custom shapes and loading bitmaps from .png files.

下一行加载时(这是一个意外的情况)锁定该文件:

The next line locks the file when loading (which is an undesired scenario):

Bitmap bmp = new Bitmap("file.png");

所以我用另一种方式来加载位图。

So I am using another way to load bitmap.

public static Bitmap LoadBitmap(string path) {
    using (Bitmap original = new Bitmap(path))
    {
        return new Bitmap(original);
    }
}

这是我想慢得多,但我不知道有什么更好的方式来加载图像,同时快速释放文件锁定。

This is I guess much slower, but I don't know any better way to load an image, while quickly releasing the file lock.

现在,绘制图像时,有方法,我使用:

Now, when drawing an image there is method that I use:

public void Draw() {
    Bitmap bmp = new Bitmap(240,320);
    Graphics g = Graphics.FromImage(bmp);

    // draw something with Graphics here.
    g.Clear(Color.Black);
    g.DrawImage(Images.CloseIcon, 16, 48);
    g.DrawImage(Images.RefreshIcon, 46, 48);
    g.FillRectangle(new SolidBrush(Color.Black), 0, 100, 240, 103);

    pictureBox.Image = bmp; 
}

然而,这似乎是某种形式的内存泄漏。如果我继续做下去的时间太长,应用程序最终崩溃。

This however seems to be some kind of a memory leak. And if I keep doing it for too long, the application eventually crashes.

因此​​,我有3个问题:

Therefore, I have 3 questions:

1)什么是从文件加载位图的更好的方法没有锁定文件?

1.) What is the better way for loading bitmaps from files without locking the file?

2)哪些对象需要手动设置在Draw()函数(及顺序),所以没有内存泄漏和无的ObjectDisposedException投掷?

2.) What objects needs to be manually disposed in the Draw() function (and in which order) so there's no memory leak and no ObjectDisposedException throwing?

3。)如果pictureBox.Image在code的最后一行设置为BMP一样,将pictureBox.Image.Dispose()处理不仅关系到维护PictureBox的资源在图像配或底层位图设置呢?

3.) If pictureBox.Image is set to bmp, like in the last line of the code, would pictureBox.Image.Dispose() dispose only resources related to maintaining the pictureBox.Image or the underlying Bitmap set to it?

推荐答案

1:
我不知道如果它工作在WM但试试这个:

1: I dont know if it works in WM but try this:

FileStream bitmapFile = new FileStream("mybitmap.bmp", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Image loaded = new Bitmap(bitmapFile);

2: SolidBrush 必须处理掉。没有为处置的一般规则。 - >每一个对象,你所实例化,实现处置必须手动处理,exept当对象的返回/ REF /输出值

2: The SolidBrush must be disposed. There is a general rule for dispose. --> "every object, instanciated by you, that implements dispose must be disposed manually, exept when the object is a return/ref/out value"

在这种情况下,最好使用使用语句

In this case it is better to use a using statement

using (new objecttodispose){ ..... } 

使用语句将确保的调用Dispose()在任何情况下(例外的例子)。

The using statement will ensure the call of Dispose() in any case (exception for example).

3。的Dispose()将释放该位图ressources

3: Dispose() will free the bitmap ressources.

这篇关于正确的方式处置图像/位图和图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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