摘要
本文章顯示範例中,如何擷取點陣圖相片,在 Microsoft Access 97 Nwind.mdb 資料庫中,並從使用動態伺服器網頁 (ASP) 的 Web 瀏覽器中檢視。若要完成這項工作,ActiveX DLL 必須建立該區域的 [存取] 和 [OLE 標題的欄位。本文章顯示如何建立這個 ActiveX DLL 中,以及如何實作它。
更多的資訊
警告: 此文件 IS,在您自己的風險中的任何使用您的程式碼所提供。Microsoft 會提供 「 現狀 」 這段程式碼,不附帶任何擔保,明示或暗示,包括但不是限於適售性以及適合某特定用途之默示擔保責任。
本文將示範如何使用 Visual Basic 來擷取儲存在 「 OLE 物件 」 欄位中的點陣圖。因為未記載的 OLE 物件儲存定義,下列程式碼會搜尋物件的 OLE 標頭,圖形的開始與一致的字元。這個方法在所有情況下,可能無法運作。
請注意,Internet Explorer 3.0 是無法顯示,則為 true 的彩色點陣圖。基於這個理由,儲存在 Access 資料庫中的點陣圖應該大於 256 色。
本文將示範如何使用 Visual Basic 來擷取儲存在 「 OLE 物件 」 欄位中的點陣圖。因為未記載的 OLE 物件儲存定義,下列程式碼會搜尋物件的 OLE 標頭,圖形的開始與一致的字元。這個方法在所有情況下,可能無法運作。
請注意,Internet Explorer 3.0 是無法顯示,則為 true 的彩色點陣圖。基於這個理由,儲存在 Access 資料庫中的點陣圖應該大於 256 色。
若要擷取的相片的逐步說明範例
- 在 Visual Basic 中建立新的專案,並使 ActiveX DLL 的專案。
- 加入參考 ActiveX Data Objects (ADO),按一下 [專案] 功能表並選取參考。選取 [Microsoft OLE DB ActiveX 資料物件 1.0 庫] 然後按一下[確定]。
- 將新模組加入至專案中,選取 [專案] 功能表,按一下 [加入模組。選取模組,按一下 [開啟]。
- 下列程式碼 (一般) (宣告) 在區段中放置的 「 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 - 將下列程式碼置於 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 - 藉由選取 [專案] 功能表中,然後按一下 [專案 1 內容] 上重新命名專案並在 [專案名稱] 欄位中輸入您的新名稱。本範例假設您命名專案"MyProject",並將參考該名稱在未來的步驟。
- 選取 「 自主式執行 」] 核取方塊。按一下 [確定]。
- 重新命名 [屬性] 窗格中的類別。本範例假設您命名的類別"MyClass 」,並指向該名稱在未來的步驟。
- 編譯的 DLL,按一下 [檔案] 功能表並選取 「 執行 MyProject.dll 」。
- 建立名為"bitmap.asp",其中包含下列的程式碼的 ASP 頁面:
<%@ 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
%> - 建立名為"BitmapTest.htm",其中包含下列的程式碼的 HTML 網頁:
<HTML>
<HEAD>
<TITLE>Bitmap Test</TITLE>
</HEAD>
<BODY>
<HR>
<img src="Bitmap.asp">
<HR>
</BODY>
</HTML>
參考
如需詳細資訊,請參閱下列文件的「Microsoft 知識庫」中的文章︰
最新的知識庫 artices 和其他有關視覺化的 asp 網頁和動態伺服器網頁之間的支援,請參閱 Microsoft 技術支援網站上的下列網頁:
173308如何儲存在 BLOB 欄位的顯示影像
最新的知識庫 artices 和其他有關視覺化的 asp 網頁和動態伺服器網頁之間的支援,請參閱 Microsoft 技術支援網站上的下列網頁: