简体   繁体   English

如果我替换PictureBox控件中的图像,我应该首先处理原始图像吗? .Net Winforms

[英]If I replace an image in a PictureBox control, should I dispose the original image first? .Net Winforms

Following on from my question here , if I replace an image in a picture box, should I dispose the original image first? 根据我的问题 ,如果我替换图片框中的图像,我应该首先处理原始图像吗?

Or, what about this situation: 或者,这种情况怎么样:

Dim bm As New Bitmap(32,32)  
bm = New Bitmap(32,32)  
bm = New Bitmap(32,32)  
bm = New Bitmap(32,32)  

Does bm need only to be disposed at the end, or should it be disposed before each re-creation? bm只需要在最后处置,还是应该在每次重新创建之前处理?


Thanks all for the answers. 谢谢大家的答案。 A big oversight there on my part. 在我身上有一个很大的疏忽。 I knew a control took care of disposing its children but It hadn't occurred to me that I should dispose an old image if I replaced it. 我知道一个控制部门负责处理它的孩子,但我没有想到如果我更换它,我应该处理一个旧图像。

Yes, you should dispose the old object before you create a new image on top of the same variable. 是的,您应该在同一变量之上创建新图像之前处置旧对象。 By creating a new image with the same variable, you are removing a reference to it. 通过使用相同变量创建新图像,您将删除对它的引用。 If there are no references to the old object, you are signifying that it should be picked up by the GC (Garbage Collector). 如果没有对旧对象的引用,则表示它应该由GC(垃圾收集器)拾取。 Although technically, this "should" eventually result in the memory being freed assuming that the finalizer makes sure that non-managed resources are taken care of, this is a big assumption (You can't even really assume that the finalizer will be called), and it causes more work for the system. 虽然从技术上讲,这“应该”最终导致内存被释放,假设终结器确保非托管资源得到处理,这是一个很大的假设(你甚至不能假设将调用终结器) ,它会为系统带来更多的工作。 Non-default finalizers causes extra work for the GC in terms of garbage collection level promotion, resulting in taking longer for the memory to be deallocated, and the number of times the GC has to run to do so. 非默认终结器会在垃圾收集级别提升方面为GC带来额外的工作,从而导致需要更长时间才能释放内存,以及GC必须运行的次数。

This is assuming that is all written to make sure the finalizer handles it. 这是假设所有写入都是为了确保终结器处理它。 Anytime an object has a Dispose method (anything which implements IDisposable which BitMap does), it should be called before removing reference to the object (falling out of scope, removing reference to the object etc.). 只要一个对象有一个Dispose方法(实现BitMap所做的IDisposable的任何东西),就应该在删除对象的引用之前调用它(超出范围,删除对象的引用等)。

Here is an article on how the Garbage Collector works in .net 这是一篇关于垃圾收集器如何在.net中工作的文章

http://www.devx.com/dotnet/Article/33167 http://www.devx.com/dotnet/Article/33167

Here is how MS says the dispose / finalizer should be implemented: 以下是MS如何实施dispose / finalizer:

http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx

When changing the image associated with a PictureBox , one should call Dispose on the image that was there if and only if nothing else is ever going to use that image. 当更改与PictureBox关联的图像时,应该在当时的图像上调用Dispose ,并且仅当没有其他任何内容将使用该图像时。 In order to know that, one would have to know where the old image came from. 为了知道这一点,人们必须知道旧图像的来源。 In some cases, the image will have been created just for the purpose of being assigned to the PictureBox . 在某些情况下,创建图像只是为了分配给PictureBox In other cases, the image might be one which is intended to be shared and/or reused. 在其他情况下,图像可以是旨在共享和/或重用的图像。 If image was created solely for the purpose of assignment to the PictureBox , it should be Dispose d if the PictureBox is disposed or given another image. 如果仅为了分配给PictureBox而创建图像,则应该在Dispose PictureBox或给出另一个图像时将其设置为Dispose d。 If the image is supposed to be shared or reused, such conditions must not cause it to be disposed. 如果图像应该被共享或重用,则这些条件不得导致它被丢弃。

The proper way to resolve such issues in general would for classes which have IDisposable properties (like the PictureBox , with Image ) to use an explicit SetImage method rather than having a mutable Image property, and for the SetImage method to include a parameter indicating whether the PictureBox should assume responsibility for disposing of it. 一般来说,解决此类问题的正确方法是,具有IDisposable属性的类(如PictureBox ,带有Image )使用显式SetImage方法而不是具有可变Image属性,并且SetImage方法包含一个参数,指示是否PictureBox应负责处理它。 Calling SetImage or Dispose on the PictureBox should call Dispose on the held image if and only if the previous SetImage call gave it that responsibility. 当且仅当前一个SetImage调用给它负责时,在PictureBox上调用SetImageDispose应该在保持的图像上调用Dispose Unfortunately, PictureBox doesn't work that way, but I would highly recommend using that as a pattern for future classes you write which hold IDisposable objects. 不幸的是, PictureBox不能这样工作,但我强烈建议将其用作您编写的包含IDisposable对象的未来类的模式。

Yes you should. 是的你应该。 It implements IDisposable. 它实现了IDisposable。
As general rule of thumb, dispose all objects that implements IDisposable. 根据一般经验法则,处置实现IDisposable的所有对象。 Do not leave it to GC. 不要把它留给GC。

Does bm need only to be disposed at the end, or should it be disposed before each re-creation? bm只需要在最后处置,还是应该在每次重新创建之前处理?

It should be disposed before each "recreation". 它应该在每次“娱乐”之前处理。 Do not confuse an object with an object reference. 不要将对象与对象引用混淆。 "new Bitmap" creates a new object. “new Bitmap”创建一个新对象。 "bm" is a reference that happens to point to that object. “bm”是恰好指向该对象的引用。 They are not the same. 他们不一样。 You are not "recreating" any object here - you are creating a new object, and then dropping all references to the previous object, meaning that i will be garbage collected some time in the (near) future. 您不是在这里“重新创建”任何对象 - 您正在创建一个新对象,然后删除对前一个对象的所有引用,这意味着我将在(近)未来的某个时间进行垃圾回收。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何以及在哪里放置旧图像? 以及将pictureBox中的图像设置为最小和最大的大小是多少? - How and where should i dispose the old image ? And to what size to set the image in the pictureBox as minimum and maximum? WinForms-UserControl.Dispose也可以在PictureBox中放置图像吗? - WinForms - Will UserControl.Dispose dispose Image in PictureBox as well? 我应该如何在 WinForms PictureBox 中将屏幕空间坐标转换为图像空间坐标? - How should I translate from screen space coordinates to image space coordinates in a WinForms PictureBox? pictureBox图像处理异常 - pictureBox image dispose exception 在不丢失图片框的情况下处理图片框图像 - Dispose of a pictureBox image without losing the pictureBox 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 为什么当我用鼠标拖动pictureBox中的图像时,它将图像恢复为原始大小? - Why when i drag with the mouse the image in the pictureBox it return the image to its original size? 如何在以拉伸模式显示图像的pictureBox中裁剪原始图像? - How can I crop original image in a pictureBox that shows image in Stretch mode? 我为什么不使用PictureBox控件? - Why should I not use the PictureBox control? 处理 Image/Bitmap 和 PictureBox 的正确方法 - Right way to dispose Image/Bitmap and PictureBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM