!!!!图像如何从Access数据库中取出并显示出来????请指点!!(有源码) (50分)

  • 主题发起人 主题发起人 unclymouse
  • 开始时间 开始时间
U

unclymouse

Unregistered / Unconfirmed
GUEST, unregistred user!
!!!!来拿分啊!!!!
,那个问题的:如何将图像从Microsoft Access数据库中取出并显示出来:
下面的这段代码是在按了“查看图像”按钮之后所触发的事件处理程序:
procedure TForm1.Button1Click(Sender: TObject);
var tempstream:TStringStream;
  tempjpeg:TJPEGImage;
begin
  try
    tempstream:=TStringStream.Create('');
    TBlobField(Query1.FieldByName('Image')).SaveToStream(tempstream);
    tempstream.Position:=0;
    tempjpeg:=TJPEGImage.Create;
    tempjpeg.LoadFromStream(tempstream);
    DBImage1.Picture.Bitmap.Assign(tempjpeg);
 finally
    tempstream.Free;
    tempjpeg.Free;
 end;
end;
这段代码的主要作用是:首先将查询结果中的JPEG图像格式数据保存到TStringStream中去,
然后设置数据指针在TStringStream中的位置为0;接着从TStringStream中读入相关数据,
并把它们赋给TDBImage.Picture.Bitmap,这样一来就实现了将数据库中所存储的JPEG格式的
数据转化为BMP格式,并在TDBImage中将图像显示出来。最后将TStringStream和TJPEGImage
这两个对象释放掉。特别要注意的是不能在设计阶段设置TDBImage的DataField属性,而只能
通过写代码的形式在运行阶段把利用流式数据所转化过来的新格式的图像数据赋给
TDBImage.Picture.Bitmap。

大侠看看这段程序错在哪里啊?我一直实现不了预览图片!!!(图片保存到ACCESS中已经实现了)
 
下面我就把我写TDataImage控件贴上来大家参考

//***************************
//
// dataimage.pas
//
// 修改 LoadPicture 可以实现其它Delphi支持的图片存储
//
//
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.

//Demo
加载照片
ADOQuery_picture.Edit;
DataImage.Picture.LoadFromFile(OpenPictureDialog.FileName);
ADOQuery_picture.Post;
删除照片
ADOQuery_picture.Edit;
DataImage.Picture := nil;
ADOQuery_picture.Post;


 
大家解答了,我马上给分的,请以下
frenking
jsxjd
qianwt
sccphi
懒虫007
你们也一定要来啊·!!!!!
 
我来了,关于我的那个程序的问题在另一个帖子里面有答案。
我用的时候没有问题,但只是为了说明用法,具体可以自己修改。
 
懒虫007:嘿嘿。。。答案在哪个贴子里啊,给个地址吧,我真的急用啊!谢谢啦》
 
在你开的另一个帖了里啊,如果不行的话可以把程序发过来我给你做。
 
呵呵。。谢谢各位,终于搞定了。
嘿嘿。。。懒虫007,以后有问题还请多多帮忙啊!
我的QQ39493690。
给分罗!!!
 
我有一些尺寸各异的图片,用DBImage显示。如何使DBImage显示出全图,而又不变形?
 

Similar threads

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