怎样将JPG、JPEG、GIF等常见的图像文件存入数据库?(100分)

  • 主题发起人 主题发起人 yukaikai
  • 开始时间 开始时间
Y

yukaikai

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样将JPG、JPEG、GIF等常见的图像文件存入数据库?

查找了一些以前的提问,只是谈到JPG格式的存入。
 
这个与图像格式无关, 都是BLOB类型的.
 
同意楼上(tseug)的,补充如下,

首先看你的数据库是什么类型,SQL SERVER, Oracle, Access,需经过如下几步:
1、定义Blob字段bPhoto
2、装载入库
with Query1 do
begin
Close;
SQL.Text := Format('SELECT BPHOTO FROM T_Image WHERE ID=%d', [11]);
Open;
Edit;
TBlobField(Fields[0]).LoadFromFile('c:/a.bmp或a.jpg,a.jpeg,a.gif等等或a.doc');
Post;
Close;
end;
 
出现:
Undeclared identifier:"TBlobField"
错误
 
if OpenPictureDialog1.Execute then
with query1 do
begin
close;
sql.Clear;
sql.Add('insert into picture (imagedata,filepath) values(:image,:filename)');
parambyname('image').LoadFromFile(OpenPictureDialog1.FileName,ftblob);
parambyname('filename').AsString:=OpenPictureDialog1.FileName;
execsql;

end;
 
var
jepg :TJpegimage;
OpenDialog :TOpenDialog;
strTemp :String;
StringStream :TStringStream;
begin
StringStream := TStringStream('');
OpenDialog := TOpenDialog.Create(Self);
if OpenDialog.Execute then
begin
strTemp := Copy(OpenDialog.FileName,Pos('.',OpenDialog.FileName),4);
if strTemp = '.jpg' then
begin
jepg := TJpegImage.Create();
jepg.LoadFromFile(文件名);
end;
jepg.SaveToStream(StringStream);

Table.insert;
Table.FieldByName(Blob).Value := StringStream.DataString;/
Table.Post;
end;
 
那么
用于读出数据库中图像数据的控件,那一个比较好呢?
当然是针对不同格式的图像:Gif、Jpg、Jpeg等
最好是数据库敏感的
 
gif在Delphi中不支持,jpg与jpeg没有感应的,可以自己写一个
 
怎样写 呢?
或者
有控件吗
 
delphi自的TDBImage控件,
 
dataimage.pas
////////////////////

unit DataImage;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls,DBCtrls, Db, DBTables, Jpeg;

type
TDataImage = class(TImage)
private
FDataLink: TFieldDataLink;
FBlobStream:tstream;
FJpgImage:tjpegimage;
FPictureLoaded:boolean;
function GetDataField:string;
procedure SetDataField(Value: string);
function GetDatasource:TDatasource;
procedure SetDatasource(Value: TDatasource);
function GetField:TField;
procedure DataChange(Sender: TObject);
procedure LoadPicture;
procedure PictureChange(Sender: TObject);
procedure UpdateData(Sender: TObject);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Field: TField read GetField;
{ Public declarations }
published
property DataField :string read GetDataField write SetDataField;
property Datasource :tDatasource read GetDatasource write SetDatasource;
{ Published declarations }
end;

procedure Register;

implementation

function TDataImage.GetDataField:string;
begin
Result := FDataLink.FieldName;
end;

procedure TDataImage.SetDataField(Value:string);
begin
if FDataLink.FieldName <> Value then
FDataLink.FieldName := Value;
end;

function TDataImage.GetDatasource:tDatasource;
begin
Result := FDataLink.DataSource;
end;

procedure TDataImage.SetDatasource(Value:tDatasource);
begin
if FDataLink.Datasource <> Value then
FDataLink.DataSource := Value;
end;

function TDataImage.GetField :TField;
begin
Result := FDataLink.Field ;
end;

procedure TDataImage.UpdateData;
begin
if Picture.Graphic is TBitmap then
begin
FJpgImage := TJpegImage.Create;
FJpgImage.Assign(Picture.Graphic)
end
else if Picture.Graphic is TJpegImage then
FJpgImage := TJpegImage(Picture.Graphic);
if (Picture.Graphic is TBitmap) or (Picture.Graphic is TJpegImage) then
begin
if Assigned(FBlobStream) then
begin
FBlobStream.Free;
FBlobStream := nil;
end;
FBlobStream := TMemoryStream.Create;
FJpgImage.SavetoStream(FBlobStream);
TBlobField(FDataLink.Field).LoadFromStream(FBlobStream);
end
else
FDataLink.Field.Clear;
end;

constructor TDataImage.Create(AOwner: TComponent);
begin
inherited create(aowner);
FDataLink:=TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnUpdateData := UpdateData;
Picture.OnChange := PictureChange;
end;

destructor TDataImage.Destroy;
begin
if Assigned(FDataLink) then
begin
FDataLink.Free;
FDataLink := nil;
end;
if Assigned(FBlobStream) then
begin
FBlobStream.Free;
FBlobStream := nil;
end;
if Assigned(FJpgImage) then
begin
FJpgImage.free;
FJpgImage:=nil;
end;
inherited;
end;

procedure TDataImage.LoadPicture;
begin
if not FPictureLoaded and Assigned(FDataLink.Field) and
FDataLink.Field.IsBlob and not TBlobField(FDataLink.Field).IsNull then
begin
if Assigned(FBlobStream) then
begin
FBlobStream.Free;
FBlobStream := nil;
end;
FBlobStream := TBlobStream.Create(TBlobField(FDataLink.Field), bmRead);
try
FJpgImage := TJpegImage.Create;
FJpgImage.LoadFromStream(FBlobStream);
Picture.Graphic := FJpgImage;
Paint;
except
try
Picture.Assign(FDataLink.Field);
Paint;
except
end;
end;
end;
end;

procedure TDataImage.DataChange(Sender: TObject);
begin
if not (FDataLink.DataSource.DataSet.State in [dsInsert,dsEdit]) then
begin
Picture.Graphic := nil;
FPictureLoaded := False;
LoadPicture;
end;
end;

procedure TDataImage.PictureChange;
begin
if Assigned(FDataLink.DataSet) and (FDataLink.DataSet.State in [dsInsert,dsEdit]) then
FDataLink.Modified;
FPictureLoaded := True;
Invalidate;
end;

procedure Register;
begin
RegisterComponents('Samples', [TDataImage]);
end;

end.
 

Similar threads

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