这是我在人事系统com+三层中的应用示例,看了以前的帖子抄的
注意dbimage1的datasource,datafield为空;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DB, DBTables,jpeg, DBCtrls, ADODB, Mask,
ExtDlgs;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
ADOTable1: TADOTable;
ADOConnection1: TADOConnection;
DataSource1: TDataSource;
Button2: TButton;
Edit1: TEdit;
Edit2: TEdit;
Panel2: TPanel;
DBImage1: TDBImage;
ADOQuery1: TADOQuery;
DBEdit1: TDBEdit;
DBEdit2: TDBEdit;
Button3: TButton;
Button4: TButton;
Panel3: TPanel;
Image1: TImage;
OpenPictureDialog1: TOpenPictureDialog;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//保存
procedure TForm1.Button1Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MyJPEG := TJPEGImage.Create;
try
IF Image1.Picture.Graphic<>NIL then
with MyJPEGdo
begin
Assign(Image1.Picture.Graphic);
MS:=TMemoryStream.create;
SaveToStream(MS);
MS.Position:=0;
ADOTable1.Open;
ADOTable1.Append;
ADOTable1.FieldbyName('p_no').asstring:=edit1.Text;
ADOTable1.FieldbyName('p_na').asstring:=edit2.Text;
TBlobField(ADOTable1.FieldbyName('photos')).LoadFromStream(MS);
ADOTable1.Post;
// messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
end;
finally
MyJPEG.Free;
edit1.Text:='';
edit2.Text:='';
Image1.Picture:=nil;
end;
end;
//显示
procedure TForm1.Button3Click(Sender: TObject);
var tempstream:TStringStream;
tempjpeg:TJPEGImage;
begin
try
ADOQuery1.Open;
tempstream:=TStringStream.Create(' ');
TBlobField(ADOQuery1.FieldByName('photos')).SaveToStream(tempstream);
tempstream.Position:=0;
tempjpeg:=TJPEGImage.Create;
tempjpeg.LoadFromStream(tempstream);
DBImage1.Picture.Bitmap.Assign(tempjpeg);
finally
tempstream.Free;
tempjpeg.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//调入图片
try
// OpenPictureDialog1.Filter := 'BMP files (*.bmp)|*.BMP|JPG files (*.jpg,*.jpeg)|*.JPG';
if OpenPictureDialog1.Execute then
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
except
showmessage('图片调入错误!');
abort;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
//下笔
var tempstream:TStringStream;
tempjpeg:TJPEGImage;
begin
try
ADOQuery1.next;
tempstream:=TStringStream.Create(' ');
TBlobField(ADOQuery1.FieldByName('photos')).SaveToStream(tempstream);
tempstream.Position:=0;
tempjpeg:=TJPEGImage.Create;
tempjpeg.LoadFromStream(tempstream);
DBImage1.Picture.Bitmap.Assign(tempjpeg);
finally
tempstream.Free;
tempjpeg.Free;
end;
end;
end.