在delphi中如何取出数据库中的图片显示在image上
推荐于2017-10-09
展开全部
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB,jpeg, DBCtrls, Mask;type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
img1: TImage;
btn1: TBitBtn;
btn2: TBitBtn;
OpenDialog1: TOpenDialog;
lbl1: TLabel;
edt1: TEdit;
lbl2: TLabel;
edt2: TEdit;
dbnvgr1: TDBNavigator;
ds1: TDataSource;
qry2: TADOQuery;
img2: TImage;
btn3: TBitBtn;
procedure FormShow(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
ms:=TMemoryStream.Create;
jpg:=TJPEGImage.Create;
jpg.Assign(img1.Picture.Graphic);
jpg.SaveToStream(ms);
ms.Position:=0;
qry1.SQL.Clear;
qry1.SQL.Add('Insert Into TIMG(PNAME,PINFO,PPHOTO) values(:pname,:pinfo,:pphoto)');
qry1.Parameters.ParamByName('pname').Value:=edt1.Text;
qry1.Parameters.ParamByName('pinfo').Value:=edt2.Text;
qry1.Parameters.ParamByName('pphoto').LoadFromStream(ms,ftBlob);
qry1.ExecSQL;
jpg.Free;
ms.Free;
end;procedure TForm1.btn2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
img1.Picture.LoadFromFile(OpenDialog1.FileName);
end;procedure TForm1.btn3Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
if not qry2.FieldByName('PPHOTO').IsNull then
begin
ms:=TMemoryStream.Create;
jpg:=TJPEGImage.Create;
TBlobField(qry2.FieldByName('PPHOTO')).SaveToStream(ms);
ms.Position:=0;
jpg.LoadFromStream(ms);
img2.Picture.Assign(jpg);
end;
end;procedure TForm1.dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
begin
btn3.Click;
end;procedure TForm1.FormShow(Sender: TObject);
begin
con1.Close;
con1.ConnectionString:='Driver=Firebird/Interbase(r) driver;UID=SYSDBA;PWD'+
'=123456;Dbname='+ExtractFilePath(Application.ExeName)+'IMG.FDB';
con1.LoginPrompt:=False;
con1.Connected:=True; qry2.Active:=True;
//img2.DataField:='PPHOTO';
end;end. 我用的是Firebird数据库,存放图片的字段名是PPHOTO,数据类型是BLOB,其他数据库应该也差不多。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, DB, ADODB,jpeg, DBCtrls, Mask;type
TForm1 = class(TForm)
con1: TADOConnection;
qry1: TADOQuery;
img1: TImage;
btn1: TBitBtn;
btn2: TBitBtn;
OpenDialog1: TOpenDialog;
lbl1: TLabel;
edt1: TEdit;
lbl2: TLabel;
edt2: TEdit;
dbnvgr1: TDBNavigator;
ds1: TDataSource;
qry2: TADOQuery;
img2: TImage;
btn3: TBitBtn;
procedure FormShow(Sender: TObject);
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
procedure dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
ms:=TMemoryStream.Create;
jpg:=TJPEGImage.Create;
jpg.Assign(img1.Picture.Graphic);
jpg.SaveToStream(ms);
ms.Position:=0;
qry1.SQL.Clear;
qry1.SQL.Add('Insert Into TIMG(PNAME,PINFO,PPHOTO) values(:pname,:pinfo,:pphoto)');
qry1.Parameters.ParamByName('pname').Value:=edt1.Text;
qry1.Parameters.ParamByName('pinfo').Value:=edt2.Text;
qry1.Parameters.ParamByName('pphoto').LoadFromStream(ms,ftBlob);
qry1.ExecSQL;
jpg.Free;
ms.Free;
end;procedure TForm1.btn2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
img1.Picture.LoadFromFile(OpenDialog1.FileName);
end;procedure TForm1.btn3Click(Sender: TObject);
var
ms:TMemoryStream;
jpg:TJPEGImage;
begin
if not qry2.FieldByName('PPHOTO').IsNull then
begin
ms:=TMemoryStream.Create;
jpg:=TJPEGImage.Create;
TBlobField(qry2.FieldByName('PPHOTO')).SaveToStream(ms);
ms.Position:=0;
jpg.LoadFromStream(ms);
img2.Picture.Assign(jpg);
end;
end;procedure TForm1.dbnvgr1Click(Sender: TObject; Button: TNavigateBtn);
begin
btn3.Click;
end;procedure TForm1.FormShow(Sender: TObject);
begin
con1.Close;
con1.ConnectionString:='Driver=Firebird/Interbase(r) driver;UID=SYSDBA;PWD'+
'=123456;Dbname='+ExtractFilePath(Application.ExeName)+'IMG.FDB';
con1.LoginPrompt:=False;
con1.Connected:=True; qry2.Active:=True;
//img2.DataField:='PPHOTO';
end;end. 我用的是Firebird数据库,存放图片的字段名是PPHOTO,数据类型是BLOB,其他数据库应该也差不多。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询