存取图片是很简单的,网上代码很多,下面是转自DelphiFans的代码
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,
hoto)'); //進行插入操作
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;
----------------------------------------------------------------
Var
OleStream : TMemoryStream;
begin
// OleContainer1
OleStream := TMemoryStream.Create;
try
(Table1.FieldByName('Text') as TBlobField).SaveToStream(OleStream);
OleStream.Position := 0;
OleContainer1.LoadFromStream(OleStream);
OleContainer1.DoVerb(1); //显示
finally
OleStream.Free;
end;
end;
----------------------------------
我们这里就比较简单了,OleContainer1.SaveToStream(MS); 然后把这个MS存到数据库就行了
读取的时候OleContainer1.LoadFromStream(MS);
只是第一次打开word的时候,也就是你从文件读取放到OleContainer的时候不能OleContainer1.LoadFromStream(MS);
而只能用如下的方法
procedure TOleContainer.LoadAsDocument(const FileName: string);
var
Stream: TFileStream;
DataHandle: HGLOBAL;
Buffer: Pointer;
begin
Stream := TFileStream.Create(FileName, fmOpenRead);
try
DataHandle := GlobalAlloc(GMEM_MOVEABLE, Stream.Size);
if DataHandle = 0 then OutOfMemoryError;
try
Buffer := GlobalLock(DataHandle);
try
Stream.Read(Buffer^, Stream.Size);
finally
GlobalUnlock(DataHandle);
end;
OleCheck(CreateILockBytesOnHGlobal(DataHandle, True, FLockBytes));
DataHandle := 0;
OleCheck(StgOpenStorageOnILockBytes(FLockBytes, nil, STGM_READWRITE or
STGM_SHARE_EXCLUSIVE, nil, 0, FStorage));
OleCheck(OleLoad(FStorage, IOleObject, Self, FOleObject));
FDrawAspect := 1;
InitObject;
UpdateView;
except
if DataHandle <> 0 then GlobalFree(DataHandle);
DestroyObject;
raise;
end;
finally
Stream.Free;
end;
end;
实际上就是多了一个文件头,如果还有问题就qq联系,