danielhordern.com

BitmapCom - an ASP component for manipulating bitmaps on the server. Developed in Visual C++ / ATL 3 (no mfc). These pages document the development of the component as it takes place.

Being able to load a bitmap from a database field seems like a useful addition to the functionality of BitmapCom. The function prototype seems obvious enough, LoadFromADOField( ADOFieldObj ).

Implementing this functionality seems straight forward, thanks to Visual C++ 6 #import statement. Adding the following line to my source file #import "C:Program FilesCommon FilesSYSTEMADOMSADO15.DLL" no_namespace rename("EOF", "adoEOF" ) allows me to manipulate ADO COM objects via simple wrapper classes generated by the compiler. Then it is simply a matter of checking the field type to ensure the field is a binary blob field, then reading the contents of the field to a variant array.

Testing the function revealed a flaw in my logic - how do I get the bitmap into the field in the first place?. I could not figure out how to drop a bitmap into Ms-access without access helpfully (not!) wrapping the object as a "package".

The simplest solution at the time to was to add functionality to BitmapCom: Write2ADOField( ADOFieldObj, Width, Height ). A sprinkle of javascript, and the database table now contained bitmaps in the nominated field.

Testing LoadFromADOField with valid data in the field revealed that I had a problem! The bitmap still did not load. Visual Basic to the rescue! - A quick vb test program allows me to debug BitmapCom. Stepping through the Write2ADOField reveals that the bitmap is written correctly, the problem lies in LoadFromADOField. Stepping through this code show something interesting. The ADOFieldObj does not appear to support GetChunk(). Cutting a long story short, the param ADOFieldObj has been replaced by two params - ADORecordSetOb and FieldName. Internally I can access the field object via the record-set and all works as expected!!! Thanks ms! - this one had me stumped for a few hours!

Sample javascript:

    <%
    function BitmapComAdoTest()
    {
        try
        {
            // need constants in jscript
            var formatJpeg = 1;
            var formatBmp = 0;
            
            // open recordset
            var oRs;
            var sql = 'SELECT * FROM Images';
            oRs = db_OpenRecordsetServer(sql, g_DBConnectionStr);

            // load bitmap from first record
            var oBitmapCom = Server.CreateObject('BitmapComponent.BitmapCom');
            oBitmapCom.LoadFromADOField(oRs, 'Image');

            // save bitmap to a new record
            oRs.AddNew();
            
            // tell bitmap to write itself to "Image" field
            //
            oBitmapCom.Write2ADOField(oRs('Image'), 0, 0, formatJpeg );
            
            // and update modifed record
            oRs.Update();
        }
        catch(e)
        {
            Response.Write('BitmapComAdoTest(): ' + e.description + '<br>');
        }
    }
    %>