Vishal jotshi
Vishal jotshi

Reputation: 76

Free an image that is allocated in a form

I have to load an image and then delete it with another timer, but its not possible unless I release the image resource, the code is:

 pic=Image.FromFile(Directory.GetCurrentDirectory+ "s.png")

this image is to be replaced by another timer code, but that is not able to replace because I am currently showing this image s.png. How can i write s.png while showing it and then reload it? Tell me some method by which I can free the resource s.png so that other programs can modify it.

Upvotes: 1

Views: 35

Answers (1)

Mark
Mark

Reputation: 8150

Try reading the image data into memory and creating the Image from that:

Dim stream = New MemoryStream(File.ReadAllBytes(Directory.GetCurrentDirectory() & "s.png"))
Dim pic = Image.FromStream(stream)

You will need to keep the stream open for the lifetime of the Image, so when the image file is replaced and you are re-reading it, call Dispose on both stream and pic before recreating them.

Upvotes: 2

Related Questions