DELPHI存取JPEG文件到SQL Server数据库(续文)
由于网友的需要做一个档案管理系统,存取jpeg文件到sql 2000数据库中,但在dbimage中及qrdbimage中均不能正常显示jpeg的文件,此为borland的限制,虽然使用第三方控件能达到要求,但我们还是来原始的实现一下这方面的技术,其实原理十分简单,既然我们在"DELPHI存取JPEG文件到SQL Server数据库"能存储jpeg文件,也能在image中显示,但一接触db类的图象显示控件就行不通了,我们可以在jpeg文件存入数据库之前转换成bmp文件再存入数据库即可,程序运行环境delphi5+ado+sql 2000桌面版+win98se2最终版,数据库中存放图片字段的类型为image,程序源代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBCtrls, Grids, DBGrids, ADODB, ExtDlgs,jpeg, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBImage1: TDBImage;
ADOQuery1ID: TStringField;
ADOQuery1FIELD: TStringField;
ADOQuery1STYLE: TStringField;
ADOQuery1SYSTEM: TStringField;
ADOQuery1DATE: TDateTimeField;
ADOQuery1PERSON1: TStringField;
ADOQuery1PERSON2: TStringField;
ADOQuery1PERSON3: TStringField;
ADOQuery1PHOTO: TBlobField;
ADOQuery1DESCRIPTION: TStringField;
OpenPictureDialog1: TOpenPictureDialog;
Image1: TImage;
Button1: TButton;
Memo1: TMemo;
procedure DBImage1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.DBImage1Click(Sender: TObject);
var
picstream:tadoblobstream;
bmppic:tbitmap;
jpgpic:tjpegimage;
bmpstream:TMemoryStream;
begin
if form1.OpenPictureDialog1.Execute then
begin
if extractfileext(form1.openpicturedialog1.filename)='.bmp' then
begin
adoquery1.edit;
picstream:=tadoblobstream.Create(tblobfield(ADOQuery1PHOTO),bmWrite);
//注:上句代码中的ADOQuery1PHOTO是adoquery1中的固定字段photo的固定字段的name
//用户需要手工添加固定字段,也可用fields[i]来进行选择,这点在
//"DELPHI存取JPEG文件到SQL Server数据库"这篇文章中没有注释,至使许多朋友没有
//编译成功。
picstream.LoadFromFile(openpicturedialog1.filename);
picstream.Position:=0;
adoquery1.edit;
tblobfield(ADOQuery1PHOTO).loadfromstream(picstream);
adoquery1.post;
adoquery1.edit;
picstream.free;
end
else
begin
jpgpic:=tjpegimage.Create;
bmppic:=tbitmap.Create;
bmpstream:=tmemorystream.Create;
jpgpic.LoadFromFile(openpicturedialog1.filename);
bmppic.Assign(jpgpic);
bmppic.SaveToStream(bmpstream);
adoquery1.edit;
picstream:=tadoblobstream.Create(tblobfield(ADOQuery1PHOTO),bmWrite);
//注:上句代码中的ADOQuery1PHOTO是adoquery1中的固定字段photo的固定字段的name
//用户需要手工添加固定字段,也可用fields[i]来进行选择,这点在
//"DELPHI存取JPEG文件到SQL Server数据库"这篇文章中没有注释,至使许多朋友没有
//编译成功。
picstream.LoadFromStream(bmpstream);
picstream.Position:=0;
adoquery1.edit;
tblobfield(ADOQuery1PHOTO).loadfromstream(picstream);
adoquery1.post;
jpgpic.free;
bmppic.free;
bmpstream.free;
adoquery1.edit;
picstream.free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.Add(form1.openpicturedialog1.filter);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
form1.openpicturedialog1.filter:='All *.jpg;*.jpeg;*.bmp;|*.jpg;*.jpeg;*.bmp;)';
end;
end.
至此实现的功能代码如上所例,希望能对需要这方面资料的朋友有所帮助.我的联系方式为:Email:delphijl@163.com 个人主页:http://delphijl.99898.com(吉林delphi开发网) QQ:123168091(技术交流,不聊天!)。