delphi image控件显示图片,想将图片保存到access数据库中,本人access数据库中的数据类型是‘OLE 对象’
1、如何将image控件图片保存到access的OLE 对象中
2、如果不用OLE 对象对象,用什么数据类型保存(不要保存路径的形式)
//保存图片
procedure TForm1.Button2Click(Sender: TObject);
var
strm: TMemoryStream;
myjpeg: TJPEGImage;
begin
if (ExtName = '.jpg') or (ExtName = '.jpeg') or (ExtName = '.bmp') then
begin
adotable1.Edit;
if (ExtName = '.jpg') or (ExtName = '.jpeg') then
begin
strm := TMemoryStream.Create;
myjpeg := TJPEGImage.Create;
myjpeg.Assign(image1.Picture.Graphic);
myjpeg.SaveToStream(strm);
strm.Position := 0;
tblobfield(adotable1.FieldByName('img1')).LoadFromStream(strm);
strm.Free;
end;
if ExtName = '.bmp' then
begin
strm := TMemoryStream.Create;
image1.Picture.Bitmap.SaveToStream(strm);
strm.Position := 0;
tblobfield(adotable1.FieldByName('img1')).LoadFromStream(strm);
strm.Free;
end;
adotable1.FieldByName('extm').Value := ExtName;
adotable1.Post;
ShowMessage('保存图片成功!');
end
else
ShowMessage('保存图片失败!');
end;
//读取图像
procedure TForm1.Button3Click(Sender: TObject);
var
buf: TMemoryStream;
begin
if adotable1.FieldValues['img1'] <> 'NULL' then
begin
buf := TMemoryStream.Create;
try
buf.Position := 0;
tblobfield(adotable1.FieldByName('img1')).SaveToStream(buf);
buf.Position := 0;
Image1.Picture.Graphic := nil;
if (adotable1.FieldValues['extm'] = '.jpg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.jpeg') then
begin
Image1.Picture.Graphic := TJPEGImage.Create;
Image1.Picture.Graphic.LoadFromStream(buf);
end;
if (adotable1.FieldValues['extm'] = '.bmp') then
begin
Image1.Picture.Bitmap := nil;
Image1.Picture.Bitmap.LoadFromStream(buf);
end;
buf.Free;
except
ShowMessage('载入图片不成功,请检查图片类型');
end;
end;
Image2.Picture := Image1.Picture;
end;