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;