请问如何在ACCESS中存取图像 ( 积分: 50 )

  • 主题发起人 主题发起人 TimeMachine
  • 开始时间 开始时间
T

TimeMachine

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在ACCESS中存取图像?
begin
TBlobField(Table1.FieldByName('DOCPIC')).loadfromfile(opendialog1.filename);
end;

程序运行时老是出错,请问如何实现
 
不能直接这么干,要借助流来操作
解决数据库中图片的问题,关键技术如下:

在数据库上使用Image二进制字段保存,使用Stream流的方式。
创建文件流:
Word_FileStream:=TFileStream.Create(Target_Name,fmOpenWrite or fmCreate);
Word_FileStream.Position:=0;

保存到数据库的Image字段:
TBlobField(AdoQuery1.FieldByName(Column_Name)).SaveToStream(Word_FileStream);

从数据库读取文件到本地硬盘:
TBlobField(ADOQuery1.FieldByName(Column_Name)).loadfromStream(Word_FileStream);

释放文件流:
Word_FileStream.Free;


创建文件流:
Word_FileStream:=TFileStream.Create(Target_Name,fmOpenWrite or fmCreate);
Word_FileStream.Position:=0;

保存到数据库的Image字段:
TBlobField(AdoQuery1.FieldByName(Column_Name)).SaveToStream(Word_FileStream);

从数据库读取文件到本地硬盘:
TBlobField(ADOQuery1.FieldByName(Column_Name)).loadfromStream(Word_FileStream);

释放文件流:
Word_FileStream.Free;

Server中该字段的数据类型是Image,在Access中该字段的数据类型是OLE对象。...

既然你已经用TDBImage控件使用Picture.LoadFromFile装入了图片,
只要该图片字段和TDBimage控件相连了,直接用adodataset.post,就保存到了
数据库。
如果没有相连,可以直接用
Tblobfield(adodataset.fields[字段名]).loadfromfile
从文件中读,或
Tblobfield(adodataset.fields[字段名]).loadfromstream
从stream中读,
要读出该字段内容,用相应的savetofile,savetostream就行了
 
既能识别bmp又识别读jpeg,我把代码修改如下:
(建议用image,不要用dbimage控件,因为该控件只能识别bmp格式图片)
(程序已调试,运行正确 delphi7 + sql server 2000)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Grids, DBGrids, DB, ADODB, StdCtrls, DBCtrls;

type
TForm1 = class(TForm)
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
DBImage1: TDBImage;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses DateUtils,jpeg;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
with ADOQuery1 do
begin
close;
SQL.Text:= 'select * from Tpic';
ADOQuery1.Open;
end;

//DBImage1.DataSource:= DataSource1;
//DBImage1.DataField:= 'pic';
end;

//显示
procedure TForm1.Button1Click(Sender: TObject);
var
aBitmap: TBitmap;
aJPEGImage: TJPEGImage;
aStream: TStream;
head: word;
const
bmp = $4D42;
jpeg = $D8FF;
begin
aBitmap:= TBitmap.Create;
aJPEGImage:= TJPEGImage.Create;
aStream:= TMemoryStream.Create;
try
TBlobField(ADOQuery1.FieldByName('pic')).SaveToStream(aStream);

aStream.Position := 0; //一定要有这句
aStream.Read(head,2);
aStream.Position:= 0;
if head = bmp then
begin
aBitmap.LoadFromStream(aStream);
Image1.Picture.Assign(aBitmap);
end
else if head = jpeg then
begin
aJPEGImage.LoadFromStream(aStream);
Image1.Picture.Assign(aJPEGImage);
end;

finally
aBitmap.Free;
aJPEGImage.Free;
aStream.Free;
end;
end;

//添加
procedure TForm1.Button2Click(Sender: TObject);
var
aBitmap: TBitmap;
aJPEGImage: TJPEGImage;
aStream: TStream;
begin
aBitmap:= TBitmap.Create;
aJPEGImage:= TJPEGImage.Create;
aStream:= TMemoryStream.Create;

try
if OpenDialog1.Execute then
begin
if ExtractFileExt(OpenDialog1.FileName)= '.bmp'then
begin
aBitmap.LoadFromFile(OpenDialog1.FileName);
aBitmap.SaveToStream(aStream);
end
else if (ExtractFileExt(OpenDialog1.FileName)= '.jpg') or (ExtractFileExt(OpenDialog1.FileName)= '.jpeg') then
begin
aJPEGImage.LoadFromFile(OpenDialog1.FileName);;
aJPEGImage.SaveToStream(aStream);
end;

with ADOQuery1 do
begin
Append;
aStream.Position:= 0;
TBlobField(FieldByName('pic')).LoadFromStream(aStream);//出错的地方:原来是因为我用了dbimage控件,该控件只能显示bmp格式图片
Post;
end;
end;
finally
aBitmap.Free;
aJPEGImage.Free;;
aStream.Free;
end;
end;

end.
 
begin
TBlobField(Table1.FieldByName('DOCPIC')).loadfromfile(opendialog1.filename);
end;
这种存储方式是可以的,我认为是最方便的方法。至于出错的原因, 是因为数据库没有处于编辑状态。改为:
beigin
Tabel1.Edit;
TBlobField(Table1.FieldByName('DOCPIC')).loadfromfile(opendialog1.filename);
Tabel1.Post;
end;
 
这样的例子很多,和在其它数据库下操作一样的,只是在ACCESS中采用OLE字段。
 
我想问一下,图像存储是否有长度限制
 
好像是214G,差不多可以认为没有限制。
 
我用的ADO,数据库字段设置成OLE类型

query.Edit;
query.FieldByName('Photo').Assign(image1.Picture.Graphic);
query.Post;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部