向SQL数据库中读、写图片之我的实例
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);
mStream.Position :=0;
jpgfile.LoadFromStream(MStream);
image2.Picture.Assign(JpgFile);
end
else begin
image2.Picture :=nil;
end;
end;
//另:如果是BMP图片得这样处理
if //是BMP //BMP型图像数据
begin
bitmap := tbitmap.Create ;
try
bitmap.LoadFromStream(strm);
image1.Picture.Graphic := bitmap;
finally
bitmap.Free;
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);
mStream.Position :=0;
jpgfile.LoadFromStream(MStream);
image2.Picture.Assign(JpgFile);
end
else begin
image2.Picture :=nil;
end;
end;
//另:如果是BMP图片得这样处理
if //是BMP //BMP型图像数据
begin
bitmap := tbitmap.Create ;
try
bitmap.LoadFromStream(strm);
image1.Picture.Graphic := bitmap;
finally
bitmap.Free;