先把下面的组件装一下,
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.