Howto:retrieving Bitmap from Access and displaying in Web Page
--------------------------------------------------------------------------------
The information in this article applies to:
Active Server Pages
Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0
ActiveX Data Objects (ADO), versions 1.0, 1.5, 2.0, 2.1 SP2, 2.5
Microsoft Internet Information Server versions 4.0, 5.0
Microsoft Data Access Components version 2.5
SUMMARY
This is article shows by example and extract the bitmap in the Microsoft Access photos
database, and view them from a Web browser using Active Server Pages (ASP). In order to accomplish this
Task, an ActiveX DLL must is created that strips the Access and OLE headers from the field. This article
Shows how to create the ActiveX DLL, and how to implement it.
More information
Warning:any with the "CODE provided in this ARTICLE" 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 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 is not work in all
circumstances.
Be aware the Internet Explorer 3.0 is unable to display true color bitmaps. For this reason, the bitmaps
stored in the Access database should is no higher than 256 colors.
Step-by-Step Example to Extract the Photos
Create a new project in Visual Basic and make the project an ActiveX DLL.
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.
Add a new module to the project by selecting the Project menu and clicking Add Module. Select Module and
Click Open.
Place the following code in the [general] (Declarations) section of MODULE1. Bas:
' Enter the following Declare statement as one single:
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
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 ' 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 The OLE header to get the bitmap header.
Buffer = ""
For i = Objectoffset to Objectoffset + 512
Buffer = buffer & CHR (ARR (i))
Next I
' Make sure ' class of the ' object ' a Paint Brush object
If Mid (Buffer, 6) = "Pbrush" Then
Bitmapheaderoffset = InStr (Buffer, "BM")
If bitmapheaderoffset > 0 Then
' Calculate the beginning of the bitmap
Bitmapoffset = Objectoffset + BitmapHeaderOffset-1
' Move the bitmap to 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
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 so you named the project "MyProject" and would
Refer to, name in future steps.
Make the project Apartment Model threaded by selecting the "Unattended Execution" check box. Click OK.
Rename the Class in the property Pane. This example assumes so you named the class "MyClass" and refers
To this name in future steps.
Compile the DLL by clicking the File menu and selecting ' Make MyProject.dll. '
Create an ASP page named "bitmap.asp" that contains the following code:
<%@ language= "VBSCRIPT"%>
<%
' You are need to set up a System DSN named ' Nwind ' which 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
%>
Create an HTML page named "bitmaptest.htm" that contains the following code:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.