0

I have a VB6 legacy that uses 100's of .wmf files I'm now creating the same application with Visual Studio 2019 VB.NET I am experiencing a problem with dynamically changing a picturebox from the default .wmf file to a new one. I've tested the code to change to a .jpg image with no problem. (see code below - the output replaces the original .wmf image and then toggles between blank and the .jpg image)

Static Toggle As Boolean
Dim FName As String = ""
If Toggle Then
    FName = "D:\VB6\06ST.jpg"
Else
    FName = "D:\VB6\06ST.wmf"
End If
Toggle = Not Toggle
PictureBox1.Image = Image.FromFile(FName)

Is there a way to overcome this problem?

Kenny
  • 3
  • 2
  • Don't keeping creating Image objects from the same files over and over and never disposing them. Just create two Image objects at the outset, switch between them as needed and then dispose them when you're done. – jmcilhinney May 02 '21 at 13:32
  • Checking the docs https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromfile?view=net-5.0#System_Drawing_Image_FromFile_System_String_ I learned 2 things of interest. First .wmf is not supported, second "The file remains locked until the Image is disposed." – Mary May 02 '21 at 20:38
  • I would try two things. use the metafile subclass/constructor and if this still results in blackouts, remove the placable header (= first 22 bytes) of the .wmfs. – kiwiwings May 02 '21 at 21:42

1 Answers1

-1

Move this line:

Toggle = Not Toggle

into the else condition.

Either that or I don't understand the question.

SezMe
  • 527
  • 8
  • 24
  • Thanks - the toggle operates correctly, the problem is that the picturebox will only display the .jpg file and turns blank when the code executes for the .wmf file (even though the initial image is set to a .wmf file in the form designer and this displays correctly) – Kenny May 02 '21 at 13:36