在数据库中保存图片文件!
我的主页上有完整的示例源码和执行程序下载:http://www.aidelphi.com
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBTables, DBCtrls, ExtDlgs;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Table1NAME: TStringField;
Table1SIZE: TSmallintField;
Table1WEIGHT: TSmallintField;
Table1AREA: TStringField;
Table1BMP: TBlobField;
OpenPictureDialog1: TOpenPictureDialog;
procedure FormCreate(Sender: TObject);
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure DBGrid1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
Uses Jpeg;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Table1.Close;
Table1.DatabaseName:='DBDEMOS';
Table1.TableName:='animals.dbf';
//使用Delphi自带的例子数据库.
Table1.Open;
end;
type
TGraphicHeader = record
Count: Word; { Fixed at 1 }
HType: Word; { Fixed at $0100 }
Size: Longint; { Size not including header }
end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
Var BlobStream:TBlobStream;
BMP:TBitmap;
JPEGs:TJPEGImage;
JPEGStream:TMemoryStream;
Header:TGraphicHeader;
begin
if Field.IsBlob then
begin
if not Field.IsNull then
begin
BlobStream:=TBlobStream.Create(TBlobField(Field),bmRead);
try
BlobStream.Read(Header,Sizeof(TGraphicHeader));
//先读一个流头,这个流头中记录有图片类型和图片大小.
//对于BMP,TBlobField.Assign(方法会自动保存这个记录,如果是JPG图片,则需要编程去处理.)
if (Header.Count<>1) or (Header.Size<>(BlobStream.Size-SizeOf(Header))) then
DBGrid1.Canvas.TextOut(Rect.Left,Rect.Top,'<ERROR GRAPHIC>')
//如果数据不符,则表示是错误数据.
else
begin
if (Header.HType=$0100) then//BMP图片
begin
BMP:=TBitmap.Create;
try
BMP.LoadFromStream(BlobStream);
//从头开始后面的数据.
DBGrid1.Canvas.StretchDraw(Rect,BMP);
//绘制在表格中.
finally
BMP.Free;
end;
end else if Header.HType=$0101 then//JPEG图片.
begin
JPEGs:=TJPEGImage.Create;
JPEGStream:=TMemoryStream.Create;
try
JPEGStream.CopyFrom(BlobStream,Header.Size);
//截取图片数据.
JPEGStream.Seek(0,0);
JPEGs.LoadFromStream(JPEGStream);
//把图片数据放入图片.
DBGrid1.Canvas.StretchDraw(Rect,JPEGs);
//在表格中绘图片.
finally
JPEGStream.Free;
JPEGs.Free;
end;
end;
end;
finally
BlobStream.Free;
end;
end else
DBGrid1.Canvas.TextOut(Rect.Left,Rect.Top,'<NULL>');
//空数据.
end else
DBGrid1.DefaultDrawDataCell(Rect,Field,State);
//其他格子.
end;
procedure TForm1.DBGrid1DblClick(Sender: TObject);
Var Picture:TPicture;
BlobStream:TBlobStream;
MemStream:TMemoryStream;
Header:TGraphicHeader;
begin
if DBGrid1.SelectedField.IsBlob then
begin
if OpenPictureDialog1.Execute then
begin
try
Picture:=TPicture.Create;
try
Picture.LoadFromFile(OpenPictureDialog1.FileName);
if Picture.Graphic is TBitmap then
begin
DBGrid1.DataSource.DataSet.Edit;
TBlobField(DBGrid1.SelectedField).Assign(Picture);
//这个语句会自动处理流头的数据,其中BMP类型的Header.HType:=$0100;
DBGrid1.DataSource.DataSet.Post;
end else if Picture.Graphic is TJPEGImage then
//JPG图片.
begin
MemStream:=TMemoryStream.Create;
try
TJPEGImage(Picture.Graphic).SaveToStream(MemStream);
//存入临时流.
Header.Count:=1;
Header.HType:=$0101;
//JPG数据的Header.HType:=$0101当然也可以定为其他不等于$0100的值.
Header.Size:=MemStream.Size;
DBGrid1.DataSource.DataSet.Edit;
BlobStream:=TBlobStream.Create(TBlobField(DBGrid1.SelectedField),bmWrite);
//创建Blob流.
try
MemStream.Seek(0,0);
BlobStream.Write(Header,sizeof(Header));
//先写入流头.
BlobStream.CopyFrom(MemStream,MemStream.Size);
//写入图片数据.
finally
BlobStream.Free;
DBGrid1.DataSource.DataSet.Post;
end;
finally
MemStream.Free;
end;
end;
finally
Picture.Free;
end;
Except
end;
end;
end;
end;
end.