如何从winforms图片框中删除所有图像数据?

问题描述:

我有一个程序,允许用户打开图像并通过将鼠标拖过它来测量它。按下按钮时,图像会在图片框中打开。打开两个图像后,程序可以正常工作,但打开三个图像后不能。在第三张图像之后,测量值被严重高估。以下是我的代码中打开图像的部分:

I have a program which allows the user to open an image and measure it by dragging the mouse over it. The image opens up in a picture box when you press a button. The program works after two images are opened, but not after three images are opened. After the third image, the measurements are grossly overestimated. Here is the part of my code which opens the image:

private void openPlan_Click(object sender, EventArgs e)// open plan folder
    {
        pictureBox1.Image = null;

        // open file dialog   
        OpenFileDialog open = new OpenFileDialog();
        open.InitialDirectory = @"C:\Users\Admin\Documents\complete Lumber Estimation Tool\Plans\";
        // image filters  
        if (open.ShowDialog() == DialogResult.OK)
        {

            trackBar1.Value = 4;
            zoom = 1.0F;
            imgOriginal = null;
            // display image in picture box  
            imgOriginal = new Bitmap(open.FileName);

            pictureBox1.Image = imgOriginal;
       }
  }



这主要是windows form程序如何存储数据的问题?如果是这样,我如何确保程序没有使用上一张图片中的任何数据或参数?



我尝试过:



我试过改变picturebox1.Image = null; to pictureBox1.Image?.Dispose(); 。我还试过从流中打开图像并通过应用流释放流变量?Dispose();


Is this mainly a problem with how a windows form program stores data? If so, how do I make sure the program isn't using any data or parameters from the previous image?

What I have tried:

I have tried changing picturebox1.Image = null; to pictureBox1.Image?.Dispose(); . I also tried opening image from a stream and releasing the stream variable by applying stream?Dispose();

猜测一下,你需要看看那个代码与您的鼠标拖动代码结合 - 我怀疑您使用的是图像以前的化身中的值,而不是使用当前图像。但我们无法访问该代码,它将取决于你。



如果你无法发现它,那么你需要使用调试器来看看代码运行时到底发生了什么 - 我们根本无法为你做到这一点!
At a guess, you need to look at that code in conjunction with your mouse drag code - I suspect that you are using values from "previous incarnations" of the image, rather than using the current image. But we can't access that code, to it's going to be up to you.

If you can't spot it, then you need to use the debugger to look at exactly what is happening while your code is running - and we can't do that for you at all!