0

I am trying to load a picture from a specific place in my database to an Image, but my code is running into some problems I can't solve.

Basically I want to load an image that is specified by a unique id, in an application that's meant for different users with different accounts. The image should basically load the picture the user has previously saved.

So first I use the following query to determine the picture I need:

procedure TForm12.BitBtn1Click(Sender: TObject);
begin
  with ADOQuery7 do
  begin
    Close;
    Sql.Clear;
    Sql.Add('SELECT Profile_Picture FROM profile WHERE username='+QuotedStr(edit12.text));
    Open;
  end;
end;

Which works fine, and indeed targets the field that has the picture with the specified username. Then I use the following to load the picture into the empty TImage:

procedure TForm12.BitBtn2Click(Sender: TObject);
var
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    if ADOquery7.Active then
    begin
      TBlobField(ADOQuery7.FieldByName('Profile_Picture')).SaveToStream(AStream);
      AStream.Position := 0;
      Image6.Picture.Graphic.LoadFromStream(AStream);
    end;
  finally
    AStream.Free;
  end;

end;

But I always get an access violation error. Anyone has any ideas what the problem is? The file I load the image from is stored in an OLE Object-column.

NGLN
  • 43,011
  • 8
  • 105
  • 200
BBs
  • 33
  • 6
  • Be aware that your code is prone to [SQL injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. You can find a more funny explanation [here](http://stackoverflow.com/q/332365/859646) and a way to overcome with Delphi's ADO [here](http://stackoverflow.com/questions/16924629/parameters-in-sql-delphi-7). – JRL Sep 02 '13 at 20:34

1 Answers1

1

Then i use the following to load the picture into the empty TImage:

Then Image5.Picture is also empty and the Graphic object isn't assigned yet; you cannot call LoadFromStream on it.

You have to know the graphic type, create and load that graphic, and then assign it to the Image component, like shown in this answer for example.

Community
  • 1
  • 1
NGLN
  • 43,011
  • 8
  • 105
  • 200