//写入数据库的方法,可以用文件对话框读到FieldName
function sfSaveJpgToTable(Query:TAdoQuery;FieldName,ImageFileName:String):boolean;
var
Jpg:TJpegImage;
begin
Result:=False;
Jpg:=TJpegImage.Create;
try
with Query do
if Active and (RecordCount>0) then
begin
Jpg.LoadFromFile(ImageFileName);
if Not (State in [dsInsert,dsEdit]) then Edit;
FieldByName(FieldName).Assign(Jpg);
Post;
Result:=True;
end;
except
jpg.Free;
Exit;
end;
Jpg.Free;
end;
//读出 PhotoFieldName是数据库中图片列的名称
function sfShowJpgFromTalbe(Query:TAdoQuery;PhotoFieldName:String;Image:TImage):boolean;
var
jpg :TJpegImage;
bs :TStream;
sImgFile:String;
begin
Result:=False;
jpg := TJpegImage.Create;
try
IF Query.Active and (Query.RecordCount >0) then
begin
bs := Query.CreateBlobStream(Query.FieldByName(PhotoFieldName),bmRead);
Image.Picture.Assign(nil);
if (bs.Size > 0) then
begin
jpg.LoadFromStream(bs);
Image.Picture.Assign(jpg);
end else
begin
sImgFile:='../Image/NoPhoto.jpg';
if FileExists(sImgFile)
then image.Picture.LoadFromFile(sImgFile);
end;
bs.Free;
Result:=True;
end else
begin
sImgFile:='../Image/NoPhoto.jpg';
if FileExists(sImgFile)
then image.Picture.LoadFromFile(sImgFile)
else image.Picture.Assign(nil);
end;
except
//MessageDlg('错误','不是JPG文件!',mtError,[mbOk],0);
jpg.Free;
Exit;
end;
jpg.Free;
end;