准備工作:
1、在空白窗體上添加: ClIEntDataSet1、Button1
2、激活窗體的 OnCreate 事件、激活 Button1 的 OnClick 事件
實現代碼:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBClIEnt;
type
TForm1 = class(TForm)
ClientDataSet1: TClIEntDataSet;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
PicPath1 = 'c:\temp\test1.bmp'; {保證測試圖片的存在}
PicPath2 = 'c:\temp\test2.bmp';
{建立數據表並載入數據}
procedure TForm1.FormCreate(Sender: TObject);
begin
{建表}
with ClIEntDataSet1 do begin
FIEldDefs.Add('Name', ftWideString, 8);
FIEldDefs.Add('Age', ftInteger);
FIEldDefs.Add('Picture', ftGraphic);
CreateDataSet;
end;
{添加第一條數據}
ClIEntDataSet1.Append;
ClIEntDataSet1['Name'] := '張三';
ClIEntDataSet1['Age'] := 66;
TGraphicField(ClientDataSet1.FIEldByName('Picture')).LoadFromFile(PicPath1);
{添加第二條數據}
ClIEntDataSet1.Append;
ClIEntDataSet1['Name'] := '李四';
ClIEntDataSet1['Age'] := 16;
TGraphicField(ClientDataSet1.FIEldByName('Picture')).LoadFromFile(PicPath2);
end;
{讀出指定記錄中的圖片數據}
procedure TForm1.Button1Click(Sender: TObject);
var
BlobStream: TClIEntBlobStream;
GraphicField: TGraphicFIEld;
Bitmap: TBitmap;
begin
{查找姓名是李四的記錄}
if ClIEntDataSet1.Locate('Name', '李四', []) then //可以把其中的李四換成張三試試
begin
{讀取其 Picture 字段}
GraphicField := ClientDataSet1.FieldByName('Picture') as TGraphicFIEld;
{讀入到 Blob 流}
BlobStream := TClientBlobStream.Create(GraphicFIEld, bmRead);
{給一個 TBitmap 流是為了看看}
Bitmap := TBitmap.Create;
Bitmap.LoadFromStream(BlobStream);
Canvas.Draw(10, 10, Bitmap);
Bitmap.Free;
//
BlobStream.Free;
end;
end;
end.