Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to show image from db else than save it to file?

Posted on 2004-08-25
3
Medium Priority
?
302 Views
Last Modified: 2010-04-05
Now i'm saving pictures in temp.jpg after that i'm showing them in image1.picture
the code is:

procedure TForm8.DBGrid1CellClick(Column: 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.FieldByName('picture')).IsNull then  //is there a picture?
    begin
      TBlobField(AdoQuery2.fieldbyname('picture')).SaveToFile('Temp.jpg');
      Image1.Picture.LoadFromFile('Temp.jpg');
      Image1.visible := true;
    end
  else
    begin
      Image1.Picture.Bitmap.Assign(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?
0
Comment
Question by:selas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 2000 total points
ID: 11893899
saving it to a stream... should be something like (I just modified your code to save to a stream instead of a file)

var
  Jpeg:TJpegImage;
  tmp:TMemoryStream;
begin
  Jpeg:=TJpegImage.create;
  tmp:=TMemoryStream.create;
  try
    AdoQuery2.SQL.Clear;
    AdoQuery2.SQL.Add('Select picture from klientai where id = 1');
    AdoQuery2.Open;

    if not TBlobField(AdoQuery2.FieldByName('picture')).IsNull then  //is there a picture?
    begin
      TBlobField(AdoQuery2.fieldbyname('picture')).SaveToStream(tmp);
      tmp.Seek(0, soFromBeginning);
      Jpeg.LoadFromStream(tmp);
      Image1.Picture.Assign(jpeg);
      Image1.visible := true;
    end
    else
    begin
      Image1.Picture.Bitmap.Assign(Nil);  //there is no Image
      Image1.visible := false;
    end;
  finally
    jpeg.Free;
    tmp.Free
  end
end;
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 11893925
forgot to mention, make sure you add jpeg to the uses clause
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11894481
// 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(var 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(Sender: 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(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) and (FDataLink <> nil) and (AComponent = DataSource) then
    DataSource := nil;
end;

procedure TGIDBImage.SetDataField(const Value: String);
begin
  FDataLink.FieldName := Value;
end;

procedure TGIDBImage.SetDataSource(const Value: TDataSource);
begin
  FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);
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).SaveToStream(MemStream);
    MemStream.Seek(soFromBeginning, 0);
    // load the JPG and assign it
    JPG.LoadFromStream(MemStream);
    Picture.Assign(JPG);
  finally
    JPG.Free;
    MemStream.Free;
  end;
end;

end.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question