I work in C# and want to keep a reference of PictureBox.Image attached to a variable and not to an object of the variable. That means I want freely change the objects of this variable and PictureBox will always refers to the variable.
I have a class that manipulates images, merges them and returns image ImageShow
to visualize in PictureBox. Often I have to assign new Image object to a variable. When I do so, PictureBox keeps the old object, while I want it to take a new one.
class Foo
{
Image ImageShow = null;
public void FooFunc()
{
//some code
changeImageSize(new Size(600,800))
}
private void changeImageSize(Size newSize)
{
if(ImageShow != null)
ImageShow.Dispose();
ImageShow = new Bitmap(newSize.Width, newSize.Height);
}
}
When I do a code like this:
var obj = Foo();
obj.SetImage(new Bitmap(50,50));
pictureBox.Image = obj.GetImage();
obj.FooFunc();
PictureBox keeps the destroyed old Image object. So I get an error if I try to do anything with PictureBox.
Returning a new image object every time creates to many dependencies between internal code of the class and PictureBox. So it is not the best solution for me.
Simple example:
Image img;
img = new Bitmap(200, 200);
pictureBox1.Image = img;
img = new Bitmap(300, 300);
Picturebox continues to refer to the old image
pictureBox1.Image.Size = {Width = 200 Height = 200}
While I want in to refers to the img
variable and new object