前些日子为答网友写的例子。其中button2为从数据库中显示JPEG格式的图片。
对于BMP格式的图片不用如此,上面有诉。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,jpeg, DB, ADODB, DBCtrls, Grids, DBGrids, StdCtrls,
ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;{用来存放图片以备用}
Image2: TImage;{用来显示从数据库中提取出来的图片}
Button1: TButton;
Button2: TButton;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DataSource2: TDataSource;
procedure Button1Click(Sender: TObject);
procedure Button2Click(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
Image1.picture.loadfromfile('d:/work/pic.bmp'); {须保证d:/下有名为pic.bmp文件}
MyJPEG:=TJPEGImage.Create;
Try
MyJPEG.Assign(Image1.Picture.Graphic); {图像转成JPEG格式,如为JPEG图片则不必加}
MS:=TMemoryStream.Create;
MyJPEG.SaveToStream(MS);
MS.Position:=0;
ADOQuery1.Append;
ADOQuery1.FieldByName('name').Value:='1';
TBlobField(ADOQuery1.FieldByName('pic')).LoadFromStream(MS);
ADOQuery1.Post;
messagebox(getactivewindow(),'图像保存完毕!','保存',mb_ok);
MS.Free;
Finally
MyJPEG.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MS: TMemoryStream;
begin
MS:=TMemoryStream.Create;
MyJPEG:=TJPEGImage.Create;
ADOQuery1.Requery;
try
TBlobField(ADOQuery1.FieldByName('pic')).SaveToStream(MS);
MS.Position:=0;
MyJPEG.LoadFromStream(MS);
Image2.Picture.Bitmap.Assign(MyJPEG);
finally
MS.Free;
MyJPEG.Free;
end;
end;
end.