I N T R O D U C T I O N
VBDbImg is simply an example program — illustrative code, nothing more. That's why it looks the way it does, with non-graphical, native-only controls and no polish of any kind. Decidedly unshiny.
It shows how to use vanilla Visual Basic to stash an image file into a database, and conversely, to spit a database image into a file, i.e. the time-honored "suck & blow" design pattern. ;-) So why do I include the equivalent of a pop song amongst so many symphonies?
In '96, a old friend and fellow programmer drove an hour to have lunch with me. When he walked into my office, I happened to be working on the inventory area of an application for a used-car dealer, and a PictureBox was displaying an image of some vehicle. He remarked what a pain it was to have to maintain image files in Visual Basic, and what a simple matter it was to use a database for image storage in Delphi.
Of course, I asked what he was talking about, and he informed me that as far as he knew, no one had figured it out, as he hadn't uncovered anything on the sad little digital rag that passed for the Web back then. So, I pulled up AltaVista. (Google wasn't a thing yet. Literally. It didn't exist. And AltaVista wasn't Yahoo yet, either. It was still AltaVista, the Google of our day!) Anyway, my friend was right: No matter how we searched, we found nothing.
Now, I'm sure I wasn't the only one to figure out how to work with database images in VB: From time-to-time, everyone sees an "obvious" solution almost no one else can find no matter how hard they look. (In fact, this was the same guy that within the next year or two wrote a C function I needed for PowerClaim, so what comes around . . .) Yet, of all of us that had discovered the answer, it seems that not a one bothered to share the wealth. Hence, this project.
D E V E L O P M E N T
Like probably every VB3 app, this one uses the Access/Jet database engine. As I was poking around in access, I stumbled upon what was then called a BLOB data type: Binary Large OBject. Now, when I opened this sucker up today, I saw that Access identified my image field as an OLE Object, but back when I created the app, BLOB fields were used to store all kinds of binary data, including OLE Objects. So I think it's the same thing today as it was then. I didn't exactly go digging for nuances on the web since we're talking about twenty-year-old code, so you're welcome to investigate yourself and drop me a line if you want to educate me.
Anyway, the "secret" is to use the AppendChunk() and GetChunk() methods from VB's Recordset.Field object. To store the image, open the file for Binary input, read it in as a byte array, and use AppendChunk() to push it into the field. Reverse the process with GetChunk to push it back out. No more complicated than that. You can see the relevant code in FPrimary.SaveFileToDatabaseRecord() and FPrimary.ExportDatabaseImageToFile().
R E S U L T S A N D P O S T - M O R T E M
Besides being upgraded from VB3 to VB6 and switching out the original database images for some slightly newer, higher-res pics, this project is unchanged from '96. It was posted on early versions of the Hawkins Research web site, but was understandably dropped at some point, as it simply wasn't relevant to company business.
This is the way I handled images all the way through VB6: Since the code is succinct and lightning fast and I'd already written it into my personal library, I just never bothered to see if Microsoft ever provided an easier way. Surely they did, but for all you guys and gals charged with maintaining and extending twentieth-century Visual Basic applications, you're welcome!
You can download VBDbImg here.