(使用 Delphi 5 + ADOExpress)
資料提供、整理:陳國生,蔡煥麟
摘要
「如何讀取並顯示 Access 資料庫的圖形欄位資料?」已是個 FAQ,本文主旨即在說明解決此問題的方法。此方法最初是在 efg's Computer Lab 網站的一篇文章中得來,但目前該文已經在網站上消失了。如果你搜尋 Borland 新聞群組,也可以找到類似的解答,整理這篇文章只是方便大家參考。
請先確定你的電腦有安裝 Microsoft© Access 中文版,在本文中我們將使用其隨附的北風資料庫來做示範。
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb;Persist Security Info=False |
以及 CommandText 屬性:
select * from 產品類別 |
1 151C2F00020000000D000E0014002100 ../...........!. 2 FFFFFFFF4269746D617020496D616765 ....Bitmap Image 3 005061696E742E506963747572650001 .Paint.Picture.. 4 05000002000000070000005042727573 ...........PBrus 5 6800000000000000000020540000424D h......... T..BM 6 16540000000000007600000028000000 .T......v...(... 7 C0000000DF0000000100040000000000 ................ 8 A0530000CE0E0000D80E000000000000 .S.............. 9 00000000000000000000800000800000 ................ 10 00808000800000008000800080800000 ................ 11 C0C0C000808080000000FF0000FF0000 ................ 12 00FFFF00FF000000FF00FF00FFFF0000 ................ 13 FFFFFF00FF0CB0C9000B090900000A00 ................ 14 9009000000000909A09A900B09000A90 ................ 15 A00000000FFFEFFFFFFFFFFFFFFFFFCB ................ 16 9CFCFEFAFFFFFFFFEDFFFEDEFFDEFEFC ................ 17 FFFFDADA00D900009009009000000000 ................ 18 090A00090BC0000900900000000A00AC ................ 19 A0E0E0E0F0E9CA9000A9CB0C00009090 ................ 20 E0000009090B0000D009009000000900 ................ 21 009A000FFFFFFFFFFFFEFFFFFFFFFCAD ................ 22 EBDBDFDFDFFFFFFFFFEFEDFFFEFFFFFF ................ 23 FEFCAF0C9A0A0D00009A000000000000 ................ 24 0009090A000B009A9000090000900C09 ................ 25 00900900FA90ADA00090B00B00000000 ................ ... |
前面說過,DBImage 之所以無法正常顯示圖形,是因為當初該欄位的圖形資料是以 OLE 物件的格式存進去的(跟是不是 Access 資料庫沒關係),所以解決方法就是將 OLE 格式的圖形資料轉換成 DBImage 支援的 bitmap 格式,參考以下步驟:
procedure LoadImageFromField(APicture: TPicture; AField: TBlobField); var ABitmap: TBitmap; AStream: TMemoryStream; begin if AField.IsNull then Exit; AStream := TMemoryStream.Create; try AField.SaveToStream(AStream); // Skip OLE storage headeer AStream.Seek(78, soFromBeginning); ABitmap := TBitmap.Create; try ABitmap.LoadFromStream(AStream); APicture.Graphic := ABitmap; finally ABitmap.Free; end; finally AStream.Free; end; end; |
procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet); begin LoadImageFromField( Image1.Picture, DataSet.FieldByName('圖片') as TBlobField ); end; |
下載範例程式:AdoAccessImg.zip
Nov-23-2001