如何用DELPHI保存图片到数据库的OLE类型字段里,然后怎么把它读出来到TImage控件中 ( 积分: 100 )

  • 主题发起人 主题发起人 f_lxin
  • 开始时间 开始时间
F

f_lxin

Unregistered / Unconfirmed
GUEST, unregistred user!
如何用DELPHI保存图片到数据库的OLE类型字段里,然后怎么把它读出来到TImage控件中
 
用全文检索或离线包找找
 
到盒子上找找吧
有例子 保存*.bmp *.jpeg *.jpg格式的到 数据库
 
procedure savepicture;
var
BS:TADOBlobStream;
begin
Query1.Edit;
BS:=TADOBlobStream.Create(TMemoField(Query1.fieldbyname('图片')),bmWrite);
Image1.Picture.Graphic.SaveToStream(BS);
BS.Free;
Query1.Post;
end;



try
m_jpegStream:=TMemoryStream.Create;
(ADOQuery1.fieldbyname('Image') as TBlobField).SaveToStream(m_JpegStream);
m_JpegStream.Position:=0;
try
image.Picture.Graphic:=nil;
image.Picture.Graphic:=TJpegImage.Create;
Image.Picture.Graphic.LoadFromStream(m_JpegStream);
except
end;
finally
FreeAndNil(m_JpegStream);
end;
 
uses
Jpeg

打开图片:
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
image1.Picture.LoadFromFile(opendialog1.FileName );
end;

导入到数据库:
procedure TForm1.ToolButton2Click(Sender: TObject);
var
MyJPEG: TJPEGImage;
MS: TMemoryStream;
begin
MyJpeg := TJpegImage.Create;
try
MyJpeg.Assign(Image1.Picture.Graphic);
MS := TMeMoryStream.Create;
MyJpeg.SaveToStream(Ms);
Ms.Position := 0;
ADOQuery1.Edit;
TBlobField(ADOQuery1.FieldByName('Image')).LoadFromStream(MS);
ADOQuery1.Post;
finally
MyJpeg.Free;
Ms.Free;
end;
//image1.Picture.Graphic:=nil;
end;

从数据库中提取显示到image:
procedure TForm1.ToolButton3Click(Sender: TObject);
var
str:Tmemorystream;
begin
//从数据库中取出流转换成图形
if ADOQuery1.FieldByName('Image').value='' then
image2.Picture.Graphic:=nil
else
begin
try
str:=Tmemorystream.Create;
Tblobfield(ADOQuery1.FieldByName('Image')).SaveToStream(str);
str.Position:=0;
image2.Picture.Graphic:=nil;
image2.Picture.Graphic:=TJpegImage.Create; //这里是jpeg的,可以改成bmp
image2.Picture.Graphic.LoadFromStream(str);
except
Image2.Picture.Bitmap.LoadFromStream(str);
end;
str.Free;
end;
end;
 
后退
顶部