VFP handles some images differently with GDIPlus


Visual Foxpro 9 uses GDIPlus to handle certain kinds of image files, such as JPG, GIF.  Because the ability to decode image formats is now centralized in the operating system, VFP can take advantage of OS updates to file formats as well as other formats like WMF.


Sometimes, GDIPlus is just used to read the image files and the rendering is done by VFP. For some formats (such as animated GIFs or images with alpha blend (transparency)), GDIPlus is used to draw the image as well.  Because VFP caches some images as “resources”, interesting problems can arise.


The code below creates a file called temp.prg that creates a form with an image control showing an animated GIF It then builds an APP file that embeds the GIF file directly internally and runs the code. If you run the program, you see the animated GIF on a form. Close the form and try to run the program again, and VFP hangs in an infinite loop trying to close a file handle for the GIF file that’s inside the APP.


The GIF is a cached image resource that GDIPlus still holds. Simple fix: it can be released via the CLEAR RESOURCE line.


(You can use this animated GIF as a sample )


 


#define TESTGIF “d:\monster.gif”


TEXT TO ctemp noshow    && create a string with a few lines of code


      PUBLIC x as Form


      x=CREATEOBJECT(“form”)


      x.Visible=1


      x.addobject(“img”,”image”)    && add an image control and call it “img”


      x.img.picture=TESTGIF


      x.img.visible=1


ENDTEXT


STRTOFILE(ctemp,”temp.prg”)   && save as a file on disk


BUILD PROJECT temp FROM temp  && build a project


MODIFY PROJECT temp nowait          && add the GIF to the project so it’s embedded inside the target APP, EXE ,or DLL


_vfp.ActiveProject.Files.Add(TESTGIF)


_vfp.ActiveProject.Close


*CLEAR RESOURCES TESTGIF            && this line will fix the problem


BUILD APP temp FROM temp            && build the APP (or exe)


DO temp                                         && do the APP (or exe)


 


 


78397

Comments (7)

  1. Richard Kaye says:

    Calvin,

    Here’s another problem with the way that VFP hooks into GDI+. If a JPG has malformed EXIF data, VFP will refuse to display it in a standard picture object. It returns error 1108. MS Paint also will choke on this JPG from which I leap to the conclusion that VFP and Paint both take the same route through GDI+. However, Explorer, Word, VB .Net and C# .Net will ignore the bad EXIF data and happily display these images. Which leads me to my second leap of logic and that is there is more than one wya to display a JPG using GDI+.

    I found this out when I had a client, who I had recently migrated from VFP 6 to 8, called me complaining about images not showing up but only on certain systems. This, of course, resulted in quite a bit of end-user panic as they thought they had lost a large number of files. Ultimately, I used some tools (VFP and 3rd party) to identify the JPGs that had bad EXIF and remove the EXIF tags. As this was over 12,000 files out of 90,000+, it was a bit of a chore. While I now know how to work around this problem if it rears its ugly head in the future, it would be nice if VFP could call the same image display functions as other MS apps which use GDI+.

  2.  

    I received a comment on my blog VFP handles some images differently with GDIPlus

    Here’s another…

  3. Russell Campbell says:

    I’ve had problems in VFP 9 with it hanging on a build. Could this problem be caused by what you mention here? The program has no animated GIFs, but does have GIFs and other images built into the EXE. Thanks.