Windows窗体 - 图片框。如何删除图像 [英] Windows Forms - picture box. How to delete an Image

查看:207
本文介绍了Windows窗体 - 图片框。如何删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序中工作,其中有一个conbobox和一个picturebox。用户选择一个路径,并且组合框加载到2000个图像的路径,并显示第一个。当用户更改conbobox的索引时,显示的图像更改,但我不知道如何删除图像框中的图像。

I am working in an application where there is a conbobox and a picturebox. The user selects a path and the conbobox loads paths to 2000 images, and displays the first one. When the user changes the index of the conbobox the image displayed changes, but I don't know how to delete the image in the picturebox.

如果我只是覆盖图像,它不做这项工作,因为当我重复的程序崩溃,因为内存。如何在图片框中删除图片?

If I just overwrite the image it doesnt do the job, as when I do it repeatedly the program crashes because of memory. How do I delete a image Inside the picturebox?

编辑:
我做了一些更改,似乎不能再次重现错误..所以也许这是别的。但是只是为了检查,这个代码是否泄漏内存?

I made few changes and can't seem to reproduce the error again.. so maybe it was something else. but just for checking, is this code leaking memory?

提示:config是一个单例,包含一些信息,在这种情况下,图像是。

tips: config is a singleton containing where some info, in this case, where the images are.

private: System::Void comboBox_image1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
         System::String^ aux;
         DIC_config* config=DIC_config::Instance();
         if(config->images_path!=NULL){
             aux=gcnew System::String(config->images_path);
             aux=aux+"\\CAM_0\\Snap_0_0"+(this->comboBox_image1->SelectedIndex+1)+".bmp";
             System::IO::FileStream^ image_file=gcnew System::IO::FileStream(aux,System::IO::FileMode::Open,System::IO::FileAccess::Read);
             System::Drawing::Bitmap^ img = gcnew System::Drawing::Bitmap(image_file);
             this->pictureBox_image1->Image=img;
             //img->~Bitmap(); this doesnt work, deletes the image of picturebox and makes the program chrash
        }

     }


推荐答案

您必须处置图片。忘记这样做使得你的程序可能会在非垃圾收集器运行不够频繁时运行非托管内存。位图对象相当小,你可以分配数千个它们,而不会触发GC,但可能消耗大量的非托管内存的像素数据。在C ++ / CLI中使用 delete 运算符来处理对象,它会调用IDisposable :: Dispose()。

You have to dispose the old image. Forgetting to do so makes it likely your program runs out of unmanaged memory when the garbage collector doesn't run frequently enough. Bitmap objects are quite small, you can allocate thousands of them without ever triggering a GC, but can consume a lot of unmanaged memory for the pixel data. You dispose objects in C++/CLI with the delete operator, it calls IDisposable::Dispose().

请注意,您使用的FileStream也是一次性的对象。这样做需要你在位图使用时保持流打开,然后关闭它。您正确地没有处理流但忘记关闭它。很难得到正确,使用Bitmap构造函数接受文件路径的字符串更容易,因此Bitmap类管理底层流本身。修正:

Do note that the FileStream you use is also a disposable object. Doing it this way requires you to keep the stream opened while the bitmap is in use and close it afterwards. You correctly did not dispose the stream but forgot closing it. Too hard to get right, it is much easier to use the Bitmap constructor that accepts a string for the file path so the Bitmap class manages the underlying stream itself. Fix:

  aux = config->images_path;
  aux += ....;
  System::Drawing::Bitmap^ img = gcnew System::Drawing::Bitmap(aux);
  delete this->pictureBox_image1->Image;
  this->pictureBox_image1->Image = img;

这篇关于Windows窗体 - 图片框。如何删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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