处理 Image/Bitmap 和 PictureBox 的正确方法 [英] Right way to dispose Image/Bitmap and PictureBox

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

问题描述

我正在尝试开发 Windows Mobile 6(在 WF/C# 中)应用程序.只有一个窗体并且窗体上只有一个 PictureBox 对象.我在上面绘制了所有想要的控件或任何我想要的控件.

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个问题:

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 设置为bmp,就像代码的最后一行,是pictureBox.Image.Dispose() 只处理与维护pictureBox.Image 相关的资源还是底层位图设置为它?

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:我不知道它是否适用于 Windows Mobile 但试试这个:

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

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

2:必须处理SolidBrush.处置有一个一般规则.-->每个对象,由你实例化,实现处置必须手动处置,除非对象返回/引用/输出值"

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"

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

In this case it is better to use a using statement

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

using 语句将确保在任何情况下(例如例外)调用 Dispose().

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

3: Dispose() 将释放位图资源.

3: Dispose() will free the bitmap ressources.

这篇关于处理 Image/Bitmap 和 PictureBox 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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