Click here to Skip to main content
15,887,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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;
       }
  }

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();
Posted
Updated 13-Sep-18 8:06am
Comments
Dave Kreskowiak 13-Sep-18 16:18pm    
The PictureBox control doesn't retain any data about the image it's displaying.

The problem isn't in the PictureBox control, but elsewhere in your code.

1 solution

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900