Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when I try to show my image by clicking in dropdownlist image display on picture box for first time
in i try to change the picture this error occour
System.ObjectDisposedException: 'Cannot access a disposed object.
Object name: '_1630EsdDisplayWf'.'



public void ShowMyImage(String fileToDisplay, int xSize, int ySize)
		{
			PictureBox pictureBoxEsdDisplay = new PictureBox();
			Assembly asm = Assembly.GetExecutingAssembly();
			Stream stm = asm.GetManifestResourceStream("FirstprojectDCS.FilesProjectsDcs." + fileToDisplay);
			// Sets up an image object to be displayed.
		if (MyImage != null)
		{
			MyImage.Dispose();
		}

			// Stretches the image to fit the pictureBox.
			pictureBoxEsdDisplay.SizeMode = PictureBoxSizeMode.StretchImage;
			MyImage = new Bitmap(stm);
			pictureBoxEsdDisplay.ClientSize = new Size(xSize, ySize);
			pictureBoxEsdDisplay.Image = (Image)MyImage;
			Controls.Add(pictureBoxEsdDisplay);
		}


What I have tried:

image change in picture box
public void ShowMyImage(String fileToDisplay, int xSize, int ySize)
		{
			PictureBox pictureBoxEsdDisplay = new PictureBox();
			Assembly asm = Assembly.GetExecutingAssembly();
			Stream stm = asm.GetManifestResourceStream("FirstprojectDCS.FilesProjectsDcs." + fileToDisplay);
			// Sets up an image object to be displayed.
		if (MyImage != null)
		{
			MyImage.Dispose();
		}

			// Stretches the image to fit the pictureBox.
			pictureBoxEsdDisplay.SizeMode = PictureBoxSizeMode.StretchImage;
			MyImage = new Bitmap(stm);
			pictureBoxEsdDisplay.ClientSize = new Size(xSize, ySize);
			pictureBoxEsdDisplay.Image = (Image)MyImage;
			Controls.Add(pictureBoxEsdDisplay);
		}



Screen scr = Screen.PrimaryScreen;
				int temphigh = scr.Bounds.Height;
				int tempwidth = scr.Bounds.Width;
				if (ESDNO_1630 == "ESDNO1_CAUSE1")
				{

					ShowMyImage("1630-ESD-001.gif",  tempWidth,tempHeight);
				}else if (ESDNO_1630 == "ESDNO1_CAUSE2")
				{
					ShowMyImage("1630-ESD-001.gif", tempWidth, tempHeight);
				}
				else if (ESDNO_1630 == "ESDNO1_CAUSE3")
				{
					ShowMyImage("1630-ESD-001.gif", tempWidth, tempHeight);
				}
Posted
Updated 6-Mar-21 0:34am

1 solution

From that code, it's not that obvious what is happening, or where exactly you get your problem.

So the first thing to do is to use the debugger to find out exactly what is going on - run your app in the debugger, and when the error occurs it will stop and show you where - you can then use the stack trace to examine how it got to that point, and find the last bit of your code that was executing when it failed.

That's tell you exactly which line of code is having the problem.

It's to do with the way you are adding controls, and probably not removing them - so when you Dispose your image, the PictureBox you created is still referencing the disposed object and when it tries to draw itself (or do pretty much anything else) it fails.
And that means that most likely you need to either not dispose the last image, remove the control that contains it, or reuse the control instead of adding a new one each time.
 
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