在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,其他数据库应该也差不多。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式