如何把图片存入数据库,图片是(.jpg,gif)等格式。数据库表的字段为什么类型?(50分)

  • 主题发起人 主题发起人 luord
  • 开始时间 开始时间
L

luord

Unregistered / Unconfirmed
GUEST, unregistred user!
如何把图片存入数据库,图片是(.jpg,gif)等格式。数据库表的字段为什么类型?
有源代码更好!!
 
我也等待
 
Binary类型,
Load***********************
procedure TForm1.BitBtn1Click(Sender: TObject);
var
aStream1: TStream;
begin
if Table1.FieldByName('Blob').IsNull then Exit;
aStream1 := Table1.CreateBlobStream(Table1.FieldByName('Blob'), bmRead);
try
aStream1.Position := 0;
aStream1.SaveToFile('temp.jpg');
finally
aStream1.Free;
end;
end;
Post****************************
procedure TForm1.BitBtn2Click(Sender: TObject);
var
aStream1: TStream;
begin
Table1.Append;
aStream1 := Table1.CreateBlobStream(Table1.FieldByName('Blob'), bmReadWrite);
try
aStream1.LoadFromFile('temp.jpg');
finally
aStream1.Free;
end;
Table1.Post;
end;


 
利用TBlobField
例如,“图片”字段对应的TBolbField的name为TablePic,则
Table1.Edit;
TablePic.LoadFromFile(PicName);
 
下面的代码是 JPG 格式:

// 存储 JPG 图片到数据库 , 同时显示在 FOOTIMAGE 控件上 FootImage:TImage
var
tempStream:TMemoryStream;
JpgPic:TJpegImage;

S:String;
begin
try
JpgPic:=TJpegImage.Create;
tempStream:=TMemoryStream.Create;
tempStream.Clear;
Table.Edit;
JpgPic.LoadFromFile(OpenDialog.FileName);
FootImage.Picture.bitmap.assign(JpgPic);
JpgPic.SaveToStream(tempStream);
TBlobField(Table.FieldByName('图象')).LoadFromStream(tempStream) ;
Table.Post;
finally
JpgPic.Free;
tempStream.Free;
end;
end;

// 显示数据库中的 JPG 图象 ,FootImage : TImage
var
MyJpeg:TJpegImage;
MyStm:TMemoryStream;
begin

if not Table.FieldByName('图象').IsNull then
begin
try
MyJpeg:=TJpegImage.Create;
MyStm:=TMemoryStream.Create;
MyStm.Clear;
TBlobField(Table.FieldByName('图象')).SaveToStream(MyStm);
MyStm.Position:=0;
MyJpeg.LoadFromStream(MyStm);
FootImage.Picture.BitMap.Assign(MyJpeg);
finally
MyJpeg.Free;
MyStm.Free;
end;

end;
 
SQL Server用image格式,oracle用blob或long raw格式。我下面连的是SQL Server,你建一个表:
DROP TABLE YW_Pic
CREATE TABLE YW_Pic(
PicID bigint IDENTITY(1,1) PRIMARY KEY, --图片ID
PicInfo image NOT NULL) --图片信息
GO
然后用ADOConnection连接,代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, ExtCtrls, DBCtrls, DB, ADODB, OleCtnrs, StdCtrls;

type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
DataSource1: TDataSource;
DBNavigator1: TDBNavigator;
DBGrid1: TDBGrid;
OleContainer1: TOleContainer;
OpenDialog1: TOpenDialog;
btnTarget: TButton;
btnShowBlobs: TButton;
procedure btnTargetClick(Sender: TObject);
procedure btnShowBlobsClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//加载目标
procedure TForm1.btnTargetClick(Sender: TObject);
var
sFileName:String;

function BlobContentToString(const FileName:String):String;
begin
with TFileStream.Create(FileName,fmOpenRead) do
try
SetLength(Result,Size);
Read(Pointer(Result)^,Size);
finally
Free;
end;
end;
begin
if (OpenDialog1.Execute) then
begin
sFileName:=OpenDialog1.FileName;
ADODataSet1.Edit;
ADODataSet1.FieldByName('PicInfo').AsString:=
BlobContentToString(sFileName);
ADODataSet1.Post;
end;
end;

//显示Blob内容
procedure TForm1.btnShowBlobsClick(Sender: TObject);
var
sFileName:String;
BS:TADOBlobStream;
begin
BS:=TADOBlobStream.Create(TBlobField(ADODataSet1.FieldByName('PicInfo')),bmRead);
try
sFileName:=ExtractFilePath(Application.ExeName)+'tmpBlob';
sFileName:=sFileName + '.' + ADODataSet1.FieldByName('PicInfo').AsString;
BS.SaveToFile(sFileName);
OleContainer1.CreateObjectFromFile(sFileName,False);
finally
BS.Free;
end;
end;

end.
 
to luord
你试过上面哪段啊。我的怎么哪段都不行,bmp没问题的,jpg可是不行啊,
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部