请问各位:怎样在数据库的Image类型字段中插入图片?(20分)

  • 主题发起人 主题发起人 黑衣人
  • 开始时间 开始时间

黑衣人

Unregistered / Unconfirmed
GUEST, unregistred user!
如一个照片字段为image类型,在Delphi中能不能通过AppendRecord向表中插入图片?
小弟用OpenPictureDialog打开并将图片放到了控件中,下一步该怎么办?
 
没人知道,不会吧、、、
顶一下
 
kao
怎么这样啊~~~实在高手多呀~~~:(
 
在数据库中保存图片文件!

我的主页上有完整的示例源码和执行程序下载: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.
 
类似问题都被问了N遍了!
 
把TField转换为TBlobField(如:TBlobField(TQuery1.FieldByName('Field')))
用TBlobField的LoadFromStream方法存入数据库,用SaveToStream读入数据。
 
多人接受答案了。
 
我从别人那里看到有一种从剪贴板上保存的方法(记不得了),好像很简单,那位能说说?
 
procedure TForm1.Button1Click(Sender: TObject);

var
Bitmap : TBitmap;
begin
Bitmap := TBitMap.create;
try
Bitmap.LoadFromClipBoardFormat(cf_BitMap,ClipBoard.GetAsHandle(cf_Bitmap),0);
Canvas.draw(0,0,Bitmap);
finally
Bitmap.free;
end;
end;
 
后退
顶部