Programmatically find the size of an image in Visual FoxPro

Need to determine a picture's height and width in pixels? It's easier than you might think.

By Dan Macleod

A Hex Central reader writes to ask if it's possible to programmatically determine the dimensions of an image in VFP. Here's an extract from his email:

I am writing a FoxPro program to generate HTML code from data in a database. It's working fine, except that I'm stuck on how to generate the <img> tags. In particular, I want to insert the height and width attributes (in pixels) for the image files, but I can't see how to figure these out. Any ideas? - Nick

In fact, this is very easy. All you have to do is to create a native VFP image object, load the picture file into it, and read off the values in its height and width properties. You don't need to place the image on a form or make it visible; the whole thing is done in memory.

So, given a variable, lcFile, that contains the image's path and filename, here's how you could find the required numbers:

loImage = CREATEOBJECT("Image")
loImage.Picture = lcFile
lnHeight = loImage.Height
lnWidth = loImage.Width

And here is a complete function that accepts the name of the image file and returns the <img> tag, complete with height and width attributes, ready for you to insert into your HTML code:

FUNCTION FileToTag
LPARAMETERS tcFile
LOCAL loImage
loImage = CREATEOBJECT("Image")
loImage.Picture = tcFile
RETURN ;
  [<img src="] + tcFile + [" height="] + ;
  TRANSFORM(loImage.Height) + [px" width = "] + ;
  TRANSFORM(loImage.Width) + [px" alt = "" />]

The function will work equally well with JPEGs, GIFs and PNGs - in fact, any file format supported by GDI+.

As an example, if you pass test.gif as the filename, the function will return a string similar to this:

<img src="test.gif" height="367px" width = "515px" alt = "" />

To keep things simple, I haven't included any error-handling in the function (for example, to check that the filename is that of an image). Nor have I worried about paths (I've asssumed the function will find the file in VFP's search path, and that the filename doesn't need to be qualified with a path or URL within the returned string). If that's not what you want, it should be an easy matter to amend the function accordingly.

Clearing the image from memory

When you create an image object in this way, keep in mind that VFP will retain the image in memory, even after the object has been released. Often, that won't matter. But if you use this technique repeatedly within a session, you might notice a significant memory bloat. For that reason, it would be sensible to clear the image from memory each time. To do so, simply execute these two commands (my thanks to Eddie Caldwell for alerting me to this issue):

loImage.Picture = ""
CLEAR RESOURCES &lcFile

First Published: December 2011. Revised: August 2016

Please note: The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly with your sites or applications.

If you found this article useful, please tell your friends: