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:[email protected]
个人主页:http://delphijl.99898.com(吉林delphi开发网)
QQ:123168091(技术交流,不聊天!)。
本文地址:http://com.8s8s.com/it/it5322.htm