procedure TForm1.Button1Click(Sender: TObject); // 插入图片过程
var
Stream:TMemoryStream;
begin
try
Stream := TMemoryStream.Create; // 创建内存流
Image1.Picture.Graphic.SaveToStream(Stream); // 将图片保存到内存流中
adoquery1.Close;
adoquery1.SQL.Clear;
adoQuery1.SQL.Add('Insert into tbltemp (Image) values (:photo)'); // 进行插入操作
adoQuery1.Parameters.ParamByName('photo').LoadFromStream(Stream,ftBlob); // 读取保存的内存图
adoquery1.ExecSQL;
finally
Stream.Free; // 释放内存流
end;
end;
procedure TForm1.Button2Click(Sender: TObject); //读取图片过程
var
Stream:TMemoryStream;
Jpg:TjpegImage;
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT * FROM tbltemp'; // 查询图片
ADOQuery1.Open;
if not ADOQuery1.FieldByName('Image').IsNull then
begin
Stream:=TMemoryStream.Create ;
Jpg:=TjpegImage.Create ;
TBlobField(ADOQuery1.FieldByName('Image')).SaveToStream(Stream); // 显示的转换为BlobField并保存到内存流
Stream.Position :=0;
jpg.LoadFromStream(Stream); // 加载图片
image2.Picture.Assign(Jpg);
end
else
begin
image2.Picture :=nil;
end;
end;