我需要处理这个Image实例吗?

时间:2022-12-16 23:36:42

I'm making a simple Image Debugger Visualizer. Code is below. I'm not sure if i need to manually dispose of the Image instance? Because i'm making a windows Form window and the PictureBox inside that contains my dynamic image .. do i need to add some special code when the form is terminating, to dispose of this?

我正在制作一个简单的Image Debugger Visualizer。代码如下。我不确定是否需要手动处理Image实例?因为我正在制作一个Windows窗体窗口和里面的PictureBox包含我的动态图像..我需要在窗体终止时添加一些特殊代码,处理这个吗?

here's the code..

这是代码..

using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.DebuggerVisualizers;
using DebuggerVisualizers;

[assembly: DebuggerVisualizer(
    typeof (ImageDebuggerVisualizer),
    typeof (VisualizerObjectSource),
    Target = typeof (Image),
    Description = "Image Visualizer")]

namespace DebuggerVisualizers
{
    public class ImageDebuggerVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            Image image = (Image) objectProvider.GetObject();
            Form form = new Form
                           {
                               Text = ("Image Visualizer - " + image.HorizontalResolution + " " + image.VerticalResolution),
                               Width = image.Width,
                               Height = image.Height
                           };

            PictureBox pictureBox = new PictureBox {Image = image, SizeMode = PictureBoxSizeMode.AutoSize};
            form.Controls.Add(pictureBox);
            form.ShowDialog();
        }
    }
}

thanks for any help :)

谢谢你的帮助 :)

4 个解决方案

#1


2  

Change your Show method to this:

将Show方法更改为:

protected override void Show(IDialogVisualizerService windowService,
    IVisualizerObjectProvider objectProvider)        
{            
    Image image = (Image) objectProvider.GetObject();
    using (Form form = new Form())
    {            
        PictureBox pictureBox = new PictureBox();    
        pictureBox.Image = image;        
        form.Controls.Add(pictureBox); 
        form.ShowDialog();
    } 
}

The using(){} block will call Dispose on the form after it closes, which will dispose of everything on the form also.

using(){}块将在窗体关闭后调用窗体上的Dispose,它也将处理窗体上的所有内容。

#2


1  

The picture box control does not dispose of the image, so this is up to you, yes.

图片框控件不会处理图像,所以这取决于你,是的。

#3


1  

Um, I'm going to go out on a limb here and say you shouldn't dispose of it.

嗯,我要在这里走出去,说你不应该把它丢弃。

I never created a visualizer, and I don't exactly know Visual Studio does this, but it seems to me that if you dispose of an object in a visualizer, you might break the code you're debugging.

我从来没有创建过可视化工具,但我并不完全知道Visual Studio会这样做,但在我看来,如果你在可视化工具中处理一个对象,你可能会破坏你正在调试的代码。

It all comes down to this line:

这一切都归结为这一行:

Image image = (Image) objectProvider.GetObject();

If that object isn't a clone, then you will be disposing the object created by the code that's being debugged. The code won't be expecting that object to be suddenly disposed, and S will hit the fan, causing you at least to have to restart your debugging.

如果该对象不是克隆,那么您将处置由正在调试的代码创建的对象。代码不会期望该对象突然被丢弃,并且S将击中风扇,导致您至少必须重新启动调试。

I'd play it safe and NOT dispose of it. Think about it--you're debugging. That's not a long lived process. If you do leak a bitmap handle, its not the end of the world...

我会安全地玩它而不是处理掉它。想一想 - 你在调试。这不是一个漫长的过程。如果你泄漏位图句柄,它不是世界末日......

#4


0  

I think you should dispose it. It should be quite easy, just add a using() at the first line of your method (around the Image image = ... line) and end it after the form.ShowDialog().

我认为你应该处理它。它应该很简单,只需在方法的第一行添加一个using()(在Image image = ... line周围),然后在form.ShowDialog()之后结束它。

I think it is safe to dispose the image, for if you want to change the visualized object you must call one of the TransferData/TranferObject/ReplaceDat/ReplaceObject methods to send it back.

我认为处理图像是安全的,因为如果要更改可视化对象,则必须调用TransferData / TranferObject / ReplaceDat / ReplaceObject方法之一将其发送回去。

#1


2  

Change your Show method to this:

将Show方法更改为:

protected override void Show(IDialogVisualizerService windowService,
    IVisualizerObjectProvider objectProvider)        
{            
    Image image = (Image) objectProvider.GetObject();
    using (Form form = new Form())
    {            
        PictureBox pictureBox = new PictureBox();    
        pictureBox.Image = image;        
        form.Controls.Add(pictureBox); 
        form.ShowDialog();
    } 
}

The using(){} block will call Dispose on the form after it closes, which will dispose of everything on the form also.

using(){}块将在窗体关闭后调用窗体上的Dispose,它也将处理窗体上的所有内容。

#2


1  

The picture box control does not dispose of the image, so this is up to you, yes.

图片框控件不会处理图像,所以这取决于你,是的。

#3


1  

Um, I'm going to go out on a limb here and say you shouldn't dispose of it.

嗯,我要在这里走出去,说你不应该把它丢弃。

I never created a visualizer, and I don't exactly know Visual Studio does this, but it seems to me that if you dispose of an object in a visualizer, you might break the code you're debugging.

我从来没有创建过可视化工具,但我并不完全知道Visual Studio会这样做,但在我看来,如果你在可视化工具中处理一个对象,你可能会破坏你正在调试的代码。

It all comes down to this line:

这一切都归结为这一行:

Image image = (Image) objectProvider.GetObject();

If that object isn't a clone, then you will be disposing the object created by the code that's being debugged. The code won't be expecting that object to be suddenly disposed, and S will hit the fan, causing you at least to have to restart your debugging.

如果该对象不是克隆,那么您将处置由正在调试的代码创建的对象。代码不会期望该对象突然被丢弃,并且S将击中风扇,导致您至少必须重新启动调试。

I'd play it safe and NOT dispose of it. Think about it--you're debugging. That's not a long lived process. If you do leak a bitmap handle, its not the end of the world...

我会安全地玩它而不是处理掉它。想一想 - 你在调试。这不是一个漫长的过程。如果你泄漏位图句柄,它不是世界末日......

#4


0  

I think you should dispose it. It should be quite easy, just add a using() at the first line of your method (around the Image image = ... line) and end it after the form.ShowDialog().

我认为你应该处理它。它应该很简单,只需在方法的第一行添加一个using()(在Image image = ... line周围),然后在form.ShowDialog()之后结束它。

I think it is safe to dispose the image, for if you want to change the visualized object you must call one of the TransferData/TranferObject/ReplaceDat/ReplaceObject methods to send it back.

我认为处理图像是安全的,因为如果要更改可视化对象,则必须调用TransferData / TranferObject / ReplaceDat / ReplaceObject方法之一将其发送回去。