procedure TForm1.BitBtn2Click(Sender: TObject); //保存图片
var
ms :TMemoryStream;
ext :string;
begin
if edtDrNameSp.Text= ' ' then exit;
if edtDrNameTy.Text= ' ' then exit;
if edtDrNameE.Text= ' ' then exit;
if not ImageEn1.IsEmpty then
begin
ext:= Extractfileext(OpenPictureDialog1.FileName);
ms := TMemoryStream.Create;
try
try
if Uppercase(ext)= '.BMP ' then
begin
ImageEn1.SaveToStreamBMP(ms);
end
else if (Uppercase(ext)= '.JPG ') or (Uppercase(ext)= '.JPEG ') then
begin
ImageEn1.SaveToStreamJpeg(ms);
end;
adoQryTmp.Active:= false;
adoQryTmp.SQL.Clear;
adoQryTmp.SQL.Add( 'select * from drimages ');
adoQryTmp.Active:= true;
ms.Position:= 0;
adoQryTmp.Append;
adoQryTmp.FieldByName( 'drNameSp ').Value:= Trim(edtDrNameSp.Text);
adoQryTmp.FieldByName( 'drNameSpPy ').Value:= Trim(edtDrNameSpPy.Text);
adoQryTmp.FieldByName( 'drNameTy ').Value:= Trim(edtDrNameTy.Text);
adoQryTmp.FieldByName( 'drNameTyPy ').Value:= Trim(edtDrNameTyPy.Text);
adoQryTmp.FieldByName( 'drNameE ').Value:= Trim(edtDrNameE.Text);
TBlobField(adoQryTmp.FieldByName( 'drimage ')).LoadFromStream(ms);
adoQryTmp.FieldByName( 'indate ').Value:=now;
adoQryTmp.FieldByName( 'memo ').Value:= RedtMemo.Text;
if Uppercase(ext)= '.BMP ' then
begin
adoQryTmp.FieldByName( 'isbmp ').Value:= 1;
end
else if (Uppercase(ext)= '.JPG ') or (Uppercase(ext)= '.JPEG ') then
begin
adoQryTmp.FieldByName( 'isbmp ').Value:= 0;
end;
adoQryTmp.Post;
Clearedt;
finally
ms.Free;
end;
application.MessageBox( '图片保存成功! ', '提示 ',mb_OK);
adoQry.Active:= false;
adoQry.Active:= true;
except
on e:exception do
begin
application.MessageBox( '图片保存失败! ', '警告! ',mb_OK);
end;
end;
end;
end;
procedure TForm1.DBGrid1CellClick(Column: TColumn); //调出图片
var
ms :TMemoryStream;
begin
ImageEn1.SetFocus;
if adoQry.Active = false then exit;
if AdoQry.RecordCount <1 then exit;
idd:= adoQry.fieldbyname( 'id ').AsInteger;
RedtMemo.Text:= adoQry.fieldbyname( 'memo ').Value;
ms := TadoBlobStream.Create(TBlobField(adoQry.FieldByName( 'drimage ')),bmread);
try
ms.Position:= 0;
ImageEn1.Clear;
if adoQry.FieldByName( 'isbmp ').AsInteger = 1 then
begin
ImageEn1.LoadFromStreamBMP(ms);
end
else
begin
ImageEn1.LoadFromStreamJpeg(ms);
end;
finally
ms.Free;
end;
end;
另一个例子
procedure PutImageintoDB(car_id:string;tempQuery:TQuery);
var //图片保存到数据库 注意这里是JPG格式的图片
buf2,buf:pchar;
f1:file;
filelen:integer;
begin
try
buf2:=pchar(car_id);;
assignfile(f1,car_id+'.jpg');
reset(f1,1);
filelen:=filesize(f1);
buf:=allocmem(filelen);
blockread(f1,buf^,filelen);
closefile(f1);
with tempQuery do begin
close;
with SQL do begin
Add('insert into carinimage values(:cim_id,:cim_image)');
end;
end;
try
tempQuery.ParamByName('cim_id').DataType:=ftString;
tempQuery.ParamByName('cim_id').SetData(buf2);
tempQuery.ParamByName('cim_image').SetBlobData(buf,filelen);
tempQuery.ExecSQL;
except
end;
buf2:=nil;
freemem(buf);
except
end;
end;
procedure GetImagefromDB(car_id:string;tempQuery:TQuery);
begin //从数据库中取出图片
with tempQuery do begin
close;
with SQL do begin
clear;
Add('select cim_image from carinimage where cim_id='''+car_id+'''');
end;
Open;
end;
(tempQuery.FieldByName('Cim_image') as tblobfield).savetofile(car_id+'.jpg');
end;
empQuery.FieldByName('Cim_image') as tblobfield
end;