将JPG格式图片保存到ACCESS数据表中 var Ind,i:Integer; FName:string; AJPeg:TJPEGImage; temp:TBitmap; begin if opd1.Execute then begin btnAdd.Enabled:=False; Ind:=Opd1.Files.Count; for i:=0 to Ind-1 do begin FName:=opd1.Files.Strings[i]; Application.ProcessMessages; tbl1.Append; tblPictureName.AsString:=FName; if UpperCase(Copy(FName,Length(Fname)-3,4))=’.JPG’ then begin AJpeg:=TJPEGImage.Create; AJpeg.LoadFromFile(FName); Temp:=TBitmap.Create; Image1.Height:=Ajpeg.Height; Image1.Width:=Ajpeg.Width; Image1.Picture:=nil; Image1.Canvas.Draw(0,0,Ajpeg); DBImage1.Picture:=Image1.Picture; AJpeg.Free; end else tblPicture.LoadFromFile(FName); tbl1.Post; end; end; 说明: tbl1:TADOQuery; opd1:TOpenPictureDialog;tblPicture是tbl1的字段,OLE型。 uses JPEG;
function FileSizes(filename:string):integer; var f:file; begin assignfile(f,filename); reset(f,1); result:=filesize(f); closefile(f); end; procedure TForm1.BitBtn1Click(Sender: TObject); begin if OpenPictureDialog1.Execute then begin ADOTable1.append; ADOTable1.FieldByName('IMG_FILENAME').AsString:=extractfilename(OpenPictureDialog1.filename); ADOTable1.FieldByName('IMG_FILESIZE').AsInteger:=filesizes(OpenPictureDialog1.filename); TBlobField(ADOTable1.FieldByName('IMG_FILEDATA')).loadfromfile(OpenPictureDialog1.filename); ADOTable1.post; ADOTable1AfterScroll(ADOTable1); end; end;procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet); begin image1.Picture.Create; if not ADOTable1.eof then if trim(ADOTable1.FieldByName('IMG_FILENAME').asstring)<>'' then begin TBlobField(ADOTable1.FieldByName('IMG_FILEDATA')).savetofile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring); Image1.Picture.LoadFromFile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring); deletefile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring); end; end;procedure TForm1.FormCreate(Sender: TObject); begin ApplicationPath:=ExtractFilePath(Application.ExeName); if copy(ApplicationPath,length(ApplicationPath),1)<>'\' then ApplicationPath:=ApplicationPath+'\'; ADOConnection1.ConnectionString:= stringreplace(ADOConnection1.ConnectionString,'Data Source=images.mdb','Data Source='+ApplicationPath+'images.mdb',[rfReplaceAll]); ADOConnection1.Open; AdoTable1.open; end;procedure TForm1.LabelDelphiKTopClick(Sender: TObject); begin ShellExecute(application.handle,pchar('OPEN'),pchar('http://delphi.ktop.com.tw'),nil,nil,0); end;end.
var
Ind,i:Integer;
FName:string;
AJPeg:TJPEGImage;
temp:TBitmap;
begin
if opd1.Execute then
begin
btnAdd.Enabled:=False;
Ind:=Opd1.Files.Count;
for i:=0 to Ind-1 do
begin
FName:=opd1.Files.Strings[i];
Application.ProcessMessages;
tbl1.Append;
tblPictureName.AsString:=FName;
if UpperCase(Copy(FName,Length(Fname)-3,4))=’.JPG’ then
begin
AJpeg:=TJPEGImage.Create;
AJpeg.LoadFromFile(FName);
Temp:=TBitmap.Create;
Image1.Height:=Ajpeg.Height;
Image1.Width:=Ajpeg.Width;
Image1.Picture:=nil;
Image1.Canvas.Draw(0,0,Ajpeg);
DBImage1.Picture:=Image1.Picture;
AJpeg.Free;
end
else
tblPicture.LoadFromFile(FName);
tbl1.Post;
end;
end;
说明:
tbl1:TADOQuery;
opd1:TOpenPictureDialog;tblPicture是tbl1的字段,OLE型。
uses
JPEG;
因为员工可能并不多,而且照片一般不大,故存在数据库中更完整与安全,
我的一个项目就是用Access存照片的,现在我在网吧不好看具体实现了?
照片字段好像是Blob还是什么Binary类型?
照片的调出就用DBImage
var f:file;
begin
assignfile(f,filename);
reset(f,1);
result:=filesize(f);
closefile(f);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
ADOTable1.append;
ADOTable1.FieldByName('IMG_FILENAME').AsString:=extractfilename(OpenPictureDialog1.filename);
ADOTable1.FieldByName('IMG_FILESIZE').AsInteger:=filesizes(OpenPictureDialog1.filename);
TBlobField(ADOTable1.FieldByName('IMG_FILEDATA')).loadfromfile(OpenPictureDialog1.filename);
ADOTable1.post;
ADOTable1AfterScroll(ADOTable1);
end;
end;procedure TForm1.ADOTable1AfterScroll(DataSet: TDataSet);
begin
image1.Picture.Create;
if not ADOTable1.eof then
if trim(ADOTable1.FieldByName('IMG_FILENAME').asstring)<>'' then
begin
TBlobField(ADOTable1.FieldByName('IMG_FILEDATA')).savetofile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring);
Image1.Picture.LoadFromFile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring);
deletefile(ApplicationPath+ADOTable1.FieldByName('IMG_FILENAME').asstring);
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ApplicationPath:=ExtractFilePath(Application.ExeName);
if copy(ApplicationPath,length(ApplicationPath),1)<>'\' then
ApplicationPath:=ApplicationPath+'\';
ADOConnection1.ConnectionString:= stringreplace(ADOConnection1.ConnectionString,'Data Source=images.mdb','Data Source='+ApplicationPath+'images.mdb',[rfReplaceAll]);
ADOConnection1.Open;
AdoTable1.open;
end;procedure TForm1.LabelDelphiKTopClick(Sender: TObject);
begin
ShellExecute(application.handle,pchar('OPEN'),pchar('http://delphi.ktop.com.tw'),nil,nil,0);
end;end.