selas
asked on
how to show image from db else than save it to file?
Now i'm saving pictures in temp.jpg after that i'm showing them in image1.picture
the code is:
procedure TForm8.DBGrid1CellClick(Co lumn: TColumn); //NUOTRAUKOS RODYMAS
begin
Labelededit1.Text := AdoQuery1.fieldbyname('id' ).Value;
AdoQuery2.SQL.Clear;
AdoQuery2.SQL.Add('Select picture from klientai where id = '+ Labelededit1.Text +'');
AdoQuery2.Open;
if not TBlobField(AdoQuery2.Field ByName('pi cture')).I sNull then //is there a picture?
begin
TBlobField(AdoQuery2.field byname('pi cture')).S aveToFile( 'Temp.jpg' );
Image1.Picture.LoadFromFil e('Temp.jp g');
Image1.visible := true;
end
else
begin
Image1.Picture.Bitmap.Assi gn(Nil); //there is no Image
Image1.visible := false;
end;
end;
I don't want to save them in file, how else i can do it?
the code is:
procedure TForm8.DBGrid1CellClick(Co
begin
Labelededit1.Text := AdoQuery1.fieldbyname('id'
AdoQuery2.SQL.Clear;
AdoQuery2.SQL.Add('Select picture from klientai where id = '+ Labelededit1.Text +'');
AdoQuery2.Open;
if not TBlobField(AdoQuery2.Field
begin
TBlobField(AdoQuery2.field
Image1.Picture.LoadFromFil
Image1.visible := true;
end
else
begin
Image1.Picture.Bitmap.Assi
Image1.visible := false;
end;
end;
I don't want to save them in file, how else i can do it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
forgot to mention, make sure you add jpeg to the uses clause
// here is DBImage component which works with Bitmap and JPG. You can easily add support for GIF
unit GIDBImage;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DB, DBCtrls, JPEG;
type
TGIDBImage = class(TImage)
private
{ Private declarations }
FDataLink : TFieldDataLink;
function GetDataField: String;
function GetDataSource: TDataSource;
function GetField : TField;
procedure SetDataField(const Value: String);
procedure SetDataSource(const Value: TDataSource);
procedure DataChange (Sender : TObject);
protected
{ Protected declarations }
procedure Loaded; override;
procedure CMGetDataLink (var Message : TMessage); message CM_GETDATALINK;
procedure Notification (AComponent : TComponent; Operation : TOperation); override;
procedure LoadPicture; virtual;
public
{ Public declarations }
constructor Create (AOwner : TComponent); override;
destructor Destroy; override;
property Field : TField read GetField;
published
{ Published declarations }
property DataField : String read GetDataField write SetDataField;
property DataSource : TDataSource read GetDataSource write SetDataSource;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('GI Data-Aware', [TGIDBImage]);
end;
{ TGIDBImage }
procedure TGIDBImage.CMGetDataLink(v ar Message: TMessage);
begin
Message.Result := Integer(FDataLink);
end;
constructor TGIDBImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
end;
procedure TGIDBImage.DataChange(Send er: TObject);
begin
if FDataLink.Field <> nil then
LoadPicture
else
Picture.Graphic := nil;
end;
destructor TGIDBImage.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TGIDBImage.GetDataField: String;
begin
Result := FDataLink.FieldName;
end;
function TGIDBImage.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
function TGIDBImage.GetField: TField;
begin
Result := FDataLink.Field;
end;
procedure TGIDBImage.Loaded;
begin
inherited Loaded;
if (csDesigning in ComponentState) then DataChange(Self);
end;
procedure TGIDBImage.Notification(AC omponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and (AComponent = DataSource) then
DataSource := nil;
end;
procedure TGIDBImage.SetDataField(co nst Value: String);
begin
FDataLink.FieldName := Value;
end;
procedure TGIDBImage.SetDataSource(c onst Value: TDataSource);
begin
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Sel f);
end;
procedure TGIDBImage.LoadPicture;
var
IsJPG : Boolean;
JPG : TJpegImage;
MemStream : TMemoryStream;
begin
// load the picture
// check if the field is BLOB
if not Field.IsBlob then
begin
Picture.Graphic := nil;
Exit;
end;
// determine the type
with Field as TBlobField do
begin
IsJPG := Pos('JFIF', Copy(Value, 1, 10)) > 0;
end;
// if it is Bitmap - load it and exit
if not IsJPG then
begin
Picture.Assign(Field);
Exit;
end;
// load the JPG with MemoryStream
MemStream := TMemoryStream.Create;
JPG := TJPEGImage.Create;
try
TBlobField(Field).SaveToSt ream(MemSt ream);
MemStream.Seek(soFromBegin ning, 0);
// load the JPG and assign it
JPG.LoadFromStream(MemStre am);
Picture.Assign(JPG);
finally
JPG.Free;
MemStream.Free;
end;
end;
end.
unit GIDBImage;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DB, DBCtrls, JPEG;
type
TGIDBImage = class(TImage)
private
{ Private declarations }
FDataLink : TFieldDataLink;
function GetDataField: String;
function GetDataSource: TDataSource;
function GetField : TField;
procedure SetDataField(const Value: String);
procedure SetDataSource(const Value: TDataSource);
procedure DataChange (Sender : TObject);
protected
{ Protected declarations }
procedure Loaded; override;
procedure CMGetDataLink (var Message : TMessage); message CM_GETDATALINK;
procedure Notification (AComponent : TComponent; Operation : TOperation); override;
procedure LoadPicture; virtual;
public
{ Public declarations }
constructor Create (AOwner : TComponent); override;
destructor Destroy; override;
property Field : TField read GetField;
published
{ Published declarations }
property DataField : String read GetDataField write SetDataField;
property DataSource : TDataSource read GetDataSource write SetDataSource;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('GI Data-Aware', [TGIDBImage]);
end;
{ TGIDBImage }
procedure TGIDBImage.CMGetDataLink(v
begin
Message.Result := Integer(FDataLink);
end;
constructor TGIDBImage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
end;
procedure TGIDBImage.DataChange(Send
begin
if FDataLink.Field <> nil then
LoadPicture
else
Picture.Graphic := nil;
end;
destructor TGIDBImage.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TGIDBImage.GetDataField: String;
begin
Result := FDataLink.FieldName;
end;
function TGIDBImage.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
function TGIDBImage.GetField: TField;
begin
Result := FDataLink.Field;
end;
procedure TGIDBImage.Loaded;
begin
inherited Loaded;
if (csDesigning in ComponentState) then DataChange(Self);
end;
procedure TGIDBImage.Notification(AC
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and (AComponent = DataSource) then
DataSource := nil;
end;
procedure TGIDBImage.SetDataField(co
begin
FDataLink.FieldName := Value;
end;
procedure TGIDBImage.SetDataSource(c
begin
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Sel
end;
procedure TGIDBImage.LoadPicture;
var
IsJPG : Boolean;
JPG : TJpegImage;
MemStream : TMemoryStream;
begin
// load the picture
// check if the field is BLOB
if not Field.IsBlob then
begin
Picture.Graphic := nil;
Exit;
end;
// determine the type
with Field as TBlobField do
begin
IsJPG := Pos('JFIF', Copy(Value, 1, 10)) > 0;
end;
// if it is Bitmap - load it and exit
if not IsJPG then
begin
Picture.Assign(Field);
Exit;
end;
// load the JPG with MemoryStream
MemStream := TMemoryStream.Create;
JPG := TJPEGImage.Create;
try
TBlobField(Field).SaveToSt
MemStream.Seek(soFromBegin
// load the JPG and assign it
JPG.LoadFromStream(MemStre
Picture.Assign(JPG);
finally
JPG.Free;
MemStream.Free;
end;
end;
end.