各位大哥:将Jpeg图象存入数据库能读出来吗?(50分)

  • 主题发起人 主题发起人 zhrrqy
  • 开始时间 开始时间
Z

zhrrqy

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥:
将Jpeg图象存入数据库能读出来吗?如何读取?
 
可以,不过最好是自己用代码读出来。
 
能!
建议你使用dbimage方便
 
to 张无忌
代码太烦,我试过用流的,效果不好呀
 
先把下面的组件装一下,
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.
 
但jpeg比较大,是地图,bmp格式可以读出来,jpeg就不行了,语句如下:
ADOTABle1.Active:=true;
Image1.Picture.Assign(ADOTable1PostMap);
Image1.Update;
 
如果代码麻烦,那就用第三方恐件,好象DBIMAGE也可以
 
if OpenPictureDialog1.Execute then
begin
for i:=0 to OpenPictureDialog1.files.count-1 do
begin
cds_photo_show.Insert;
cds_photo_show.FieldByName('name').asstring:=ExTractFileName(OpenPictureDialog1.FileName);

DBImg_photo.Picture.LoadFromFile(OpenPictureDialog1.FileName);
DBImg_photo.CopyToClipboard;
DBImg_photo.PasteFromClipboard;
//Cds_ws_photophoto.LoadFromFile(OpenPictureDialog1.filename);
cds_photo_show.FieldByName('lx').asstring:=copy(ExtractFileExt(OpenPictureDialog1.FileName),2,3);
cds_photo_show.FieldByName('cfrq').asstring:=datetostr(date)+' '+timetostr(time);
cds_photo_show.FieldByName('wjrq').asstring:=datetimetostr(FGetFileTime(ExTractFilePath(OpenPictureDialog1.FileName)+ExTractFileName(OpenPictureDialog1.FileName),1));
if cds_wswj.FieldByName('Dh').asstring='' then
begin
cds_photo_show.Cancel;
MessageBox(Handle,'请先录入文件档号!','提示信息', MB_ICONSTOP or MB_OK);
exit;
end;
cds_photo_show.FieldByName('wjdh').asstring:=cds_wswj.FieldByName('Dh').asstring;
//AssignFile(fp,ExTractFilePath(OpenPictureDialog1.FileName)+ExTractFileName(OpenPictureDialog1.FileName));
//Reset(fp);
cds_photo_show.FieldByName('dx').asinteger:=FileLength(ExTractFilePath(OpenPictureDialog1.FileName)+ExTractFileName(OpenPictureDialog1.FileName));
//CloseFile(fp);
cds_photo_show.ApplyUpdates(-1);
end;
end;
 
多人接受答案了。
 
以上是dbimage 打开openpicturedialos所选择的多个文件
 
用流挺好用的呀,搜索以前帖子很多的
 

Similar threads

S
回复
0
查看
835
SUNSTONE的Delphi笔记
S
S
回复
0
查看
765
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部