在线咨询
QQ咨询
服务热线
服务热线:13125520620
TOP

数据库中存取图片-数据库

发布时间:2011-11-12 浏览:4873

uses jpeg;
保存Image1中的图象至数据库 :
var
Ms:TmemoryStream;
jpg:Tjpegimage;
begin
ms:=TmemoryStream.Create;
Jpg.Assign(Image1.Picture.Graphic);
Jpg.SaveToStream(Ms) ;
Ms.Position :=0;
ADOquery1.append;
TBlobField(ADOquery1.FieldByName('img')).LoadFromStream(Ms);
ADOquery1.Post;
Ms.Free ;
jpg.free;
end;
数据库中读取图象到image2中:
Var
Ms:TStringStream;
jpg:Tjpegimage;
begin
Ms:=TstringStream.Create('');
TBlobField(ADOquery1.FieldByName('img')).SaveToStream(Ms);
Ms.Position :=0;
Jpg.LoadFromStream(Ms);
Image2.Picture.Assign(Jpg);
Ms.Free;
jpg.free;
end;

例子2:

procedure TForm1.Button1Click(Sender: TObject); //插入圖片過程
var
testStream:TMemoryStream;
begin
try
testStream := TMemoryStream.Create; //創建內存流
Image1.Picture.Graphic.SaveToStream(testStream);  //將圖片保存至內存流中
adoquery1.Close;
adoquery1.SQL.Clear;
adoQuery1.SQL.Add('Insert into test (id,photo) values (:id,:photo)'); //進行插入操作
adoquery1.Parameters.ParamByName('id').Value := '003';
adoQuery1.Parameters.ParamByName('photo').LoadFromStream(testStream,ftBlob); //讀取保存的內存圖
adoquery1.ExecSQL;
finally
testStream.Free;   //釋放內存流
end;
end;

procedure TForm1.Button2Click(Sender: TObject); //讀取圖片過程
var
mStream:TMemoryStream;
JpgFile:TjpegImage;
begin
if not ADOQuery1.FieldByName('photo').IsNull then begin ;
mStream:=TMemoryStream.Create ;
JpgFile:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('photo')).SaveToStream(mStream);  //顯示的轉換為BlobFiled並保存至內存流
mStream.Position :=0;
jpgfile.LoadFromStream(MStream);
image2.Picture.Assign(JpgFile);
end
else begin
image2.Picture :=nil;
end;
end;

TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺
0
该内容对我有帮助