//保存圖片到數據庫
function imagesavetosql(dataset:TQuery;filename:String):boolean;
var
imagejpg:TJPEGImage; //jpg圖片
MyStm:TMemoryStream;
begin
result:=false;
MyStm:=TMemoryStream.Create;
imagejpg:=Tjpegimage.Create;
if filename<>'' then
begin
imagejpg.LoadFromFile(filename);
imagejpg.SaveToStream(MyStm);
MyStm.Position:=0;
TBlobField(dataset.FIEldByName('pict')).LoadFromStream(MyStm); //不可與DMImage之類控件,因為其只支持BMP
result:=true;
end;
MyStm.Free;
imagejpg.free;
end;
//界面上添加TDirectoryListBox,TGauge控件
procedure Tfrmpict.BitBtn2Click(Sender: TObject);
var
dirlist:TStringList;
i:Integer;
begin
inherited;
if chk_road.Checked then
begin
dirlist:=TStringList.Create ;
dirlist.Clear;
try
GetAllFileName(DirectoryListBox1.Directory,dirlist);
if dirlist.Count>0 then
begin
Gauge1.MinValue :=0;
Gauge1.MaxValue:= dirlist.Count-1;
for i:=0 to dirlist.Count-1 do
begin
with dm.qry_pict do
begin
Insert;
FIElds[0].AsString:=copy(dirlist.Strings[i],1,pos('.',dirlist.Strings[i])-1);
if imagesavetosql(dm.qry_pict,dirlist.Strings[i] )=false then
begin
ShowMessage('導入'+dirlist.Strings[i]+'.jpg圖片時出錯');
Abort;
end;
Post;
end; //with
Gauge1.AddProgress(1);
end; //for
end //if
else
ShowMessage('該目錄下不存在JPG類型圖片');
finally
dirlist.Free;
end;
end
else
ShowMessage('請執行路徑選取操作');
//Close;
end;