Microsoft Knowledge Base Email Alertz

(175261) - This article shows by example how to extract the bitmap photos in the Microsoft Access 97 Nwind.mdb database, and view them from a Web browser using Active Server Pages (ASP). In order to accomplish this task, an ActiveX DLL must be created that...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 175261 - Last Review: May 2, 2006 - Revision: 5.0

How To Retrieve Bitmap from Access and Display It in Web Page

This article was previously published under Q175261

On This Page

SUMMARY

This article shows by example how to extract the bitmap photos in the Microsoft Access 97 Nwind.mdb database, and view them from a Web browser using Active Server Pages (ASP). In order to accomplish this task, an ActiveX DLL must be created that strips the Access and OLE headers from the field. This article shows how to create this ActiveX DLL, and how to implement it.

MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

This article demonstrates how to use Visual Basic to retrieve a bitmap stored in an OLE Object field. Because the definition of OLE object storage is not documented, the following code searches the object's OLE header for characters consistent with the start of the graphic. This method may not work in all circumstances.

Be aware that Internet Explorer 3.0 is unable to display true color bitmaps. For this reason, the bitmaps stored in the Access database should be no higher than 256 colors.

Step-by-Step Example to Extract the Photos

  1. Create a new project in Visual Basic and make the project an ActiveX DLL.
  2. Add a reference to ActiveX Data Objects (ADO) by clicking the Project menu and selecting References. Select "Microsoft OLE DB ActiveX Data Objects 1.0 Library" and click OK.
  3. Add a new module to the project by selecting the Project menu and clicking Add Module. Select Module and click Open.
  4. Place the following code in the (general) (declarations) section of MODULE1.BAS:
          ' Enter the following Declare statement as one single line:
          Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
           (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
          Type PT
            Width As Integer
            Height As Integer
          End Type
    
          Type OBJECTHEADER
            Signature As Integer
            HeaderSize As Integer
            ObjectType As Long
            NameLen As Integer
            ClassLen As Integer
            NameOffset As Integer
            ClassOFfset As Integer
            ObjectSize As PT
            OleInfo As String * 256
          End Type
    					
  5. Place the following code in the (general) (declarations) section of CLASS1.CLS:
            Function DisplayBitmap(ByVal OleField As Variant)
            Dim Arr() As Byte
            Dim ObjHeader As OBJECTHEADER
            Dim Buffer As String
            Dim ObjectOffset As Long
            Dim BitmapOffset As Long
            Dim BitmapHeaderOffset As Integer
            Dim ArrBmp() As Byte
            Dim i As Long
    
            'Resize the array, then fill it with
            'the entire contents of the field
            ReDim Arr(OleField.ActualSize)
            Arr() = OleField.GetChunk(OleField.ActualSize)
    
            'Copy the first 19 bytes into a variable
            'of the OBJECTHEADER user defined type.
            CopyMemory ObjHeader, Arr(0), 19
    
            'Determine where the Access Header ends.
            ObjectOffset = ObjHeader.HeaderSize + 1
    
            'Grab enough bytes after the OLE header to get the bitmap header.
            Buffer = ""
            For i = ObjectOffset To ObjectOffset + 512
                Buffer = Buffer & Chr(Arr(i))
            Next i
    
            'Make sure the class of the object is a Paint Brush object
            If Mid(Buffer, 12, 6) = "PBrush" Then
                BitmapHeaderOffset = InStr(Buffer, "BM")
                If BitmapHeaderOffset > 0 Then
    
                    'Calculate the beginning of the bitmap
                    BitmapOffset = ObjectOffset + BitmapHeaderOffset - 1
    
                    'Move the bitmap into its own array
                    ReDim ArrBmp(UBound(Arr) - BitmapOffset)
                    CopyMemory ArrBmp(0), Arr(BitmapOffset), UBound(Arr) -
                     BitmapOffset + 1
    
                    'Return the bitmap
                    DisplayBitmap = ArrBmp
                End If
            End If
          End Function
    					
  6. Rename the Project by selecting the Project menu, and clicking on "Project1 Properties" and type your new name in the "Project Name" field. This example assumes that you named the project "MyProject" and will refer to that name in future steps.
  7. Select the "Unattended Execution" check box. Click OK.
  8. Rename the Class in the Property Pane. This example assumes that you named the class "MyClass" and refers to that name in future steps.
  9. Compile the DLL by clicking the File menu and selecting "Make MyProject.dll."
  10. Create an ASP page named "bitmap.asp" that contains the following code:
          <%@ LANGUAGE="VBSCRIPT" %>
          <%
          '   You need to set up a System DSN named 'NWind' that points to
          '   the Northwind.mdb database
          Set DataConn = Server.CreateObject("ADODB.Connection")
          DataConn.Open "DSN=NWind", "admin", ""
          Set cmdTemp = Server.CreateObject("ADODB.Command")
          Set RS = Server.CreateObject("ADODB.Recordset")
          cmdTemp.CommandText = "SELECT Photo FROM Employees
            WHERE EmployeeID = 1"
          cmdTemp.CommandType = 1
          Set cmdTemp.ActiveConnection = DataConn
          RS.Open cmdTemp, , 0, 1
          Response.ContentType = "image/bmp"
          Set Bitmap = Server.CreateObject("MyProject.MyClass")
          Response.BinaryWrite Bitmap.DisplayBitmap(RS("Photo"))
          RS.Close
          %>
    					
  11. Create an HTML page named "BitmapTest.htm" that contains the following code:
          <HTML>
          <HEAD>
          <TITLE>Bitmap Test</TITLE>
          </HEAD>
          <BODY>
          <HR>
          <img src="Bitmap.asp">
          <HR>
          </BODY>
          </HTML>
    					

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:
173308  (http://kbalertz.com/Feedback.aspx?kbNumber=173308/EN-US/ ) How To Displaying Images Stored in a BLOB Field

For the latest Knowledge Base artices and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/search/default.aspx?qu=vinterdev (http://support.microsoft.com/search/default.aspx?qu=vinterdev)


APPLIES TO
  • Microsoft Active Server Pages 4.0
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft ActiveX Data Objects 1.0
  • Microsoft ActiveX Data Objects 1.5
  • Microsoft ActiveX Data Objects 2.0
  • Microsoft ActiveX Data Objects 2.1 Service Pack 2
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
  • Microsoft Data Access Components 2.5
  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7
Keywords: 
kbhowto kbcodesnippet kbdatabase kbsample KB175261
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Nick - EngWah_Ooi NOSPAM-AT-NOSPAM Jabil.com Report As Irrelevant  
Written: 8/23/2004 2:18 AM
Hi,I seing a lot of coding on your pages but i confusing on how to create ASP and no clear on 10 and 11.I new on the ASP and VB for me is not in Advance.