0

This is my code; I have a table that contains blob variables and I need to retrieve it and add to a image. this is my code and it not working

procedure TForm1.Button1Click(Sender: TObject);
var mybitmap, newBitmap: TBitmap;
    str, query: string;
    sqlQRY: TSQLQuery;
    BLOB: TBlobField;
    Ts : TMemoryStream;
    bl  : TBlobData;
begin

  SQLConnection1.Open;

  mybitmap:= TBitmap.Create;
  mybitmap.Width :=25;
  mybitmap.Height := 25;

  query := 'SELECT * FROM CATEGORY_TYPES';
  sqlQRY := TSQLQuery.Create(self);
  sqlQRY.SQLConnection := SQLConnection1;
  sqlQRY.SQL.Add(query );

  sqlQRY.Open;
  while not sqlQRY.Eof do
  begin
    if not sqlQRY.FieldByName('ICON').IsNull then
    begin

      BLOB := sqlQRY.FieldByName('Icon') as TBlobField;
      ts := TMemoryStream.Create;
      BLOB.SaveToStream(ts);
      ts.Position :=0;
      mybitmap.LoadFromStream(Ts);
      Image1.Picture.Assign(mybitmap);
    end;
    sqlQRY.Next;
  end;
  sqlQRY.Close;
end;

The above code is not working; it shows an error message that bitmap is not can any one fix it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Azad
  • 5,144
  • 4
  • 28
  • 56
  • 4
    Where did you get inspiration for your code ? We shall burn that page. Follow e.g. [`this article`](http://edn.embarcadero.com/article/27462) and even though it's utter quality, you might get more lucky. Out of curiosity, what is Firemonkey database ? – TLama Dec 24 '13 at 11:11
  • It is not Firemonkey, it is Firemonky. – Free Consulting Dec 24 '13 at 11:12
  • 1
    @Free, ah, I missed that. So there is no animal at all... – TLama Dec 24 '13 at 11:16
  • @TLama i modified according to you but i still get that error message 'Bitmap image not valid' – Azad Dec 24 '13 at 11:16
  • @FreeConsulting: actually - **no, it's not** - it **IS** `Firemonkey` - [See this link](http://www.embarcadero-info.com/firemonkey/) – marc_s Dec 24 '13 at 11:21
  • are you sure that your DB table actually contains images? :) – teran Dec 24 '13 at 11:21
  • yes my DB contains images – Azad Dec 24 '13 at 11:27
  • Welcome to StackOverflow. Comments here often gets vandalized. Please [edit](http://stackoverflow.com/posts/20759647/edit) your question to include an error message. My suggestion: dump a blob and make sure it starts with proper [BITMAPFILEHEADER](http://msdn.microsoft.com/en-us/library/windows/desktop/dd183374). – Free Consulting Dec 24 '13 at 11:28
  • Try `(sqlQRY.FieldByName('Icon') as TBlobField).SaveToFile('some name');` and then check in HEX viewer whether saved file is really BMP format or some other picture format or even some non-picture – Arioch 'The Dec 24 '13 at 12:21
  • 1
    What is "Firemonkey database" ? – Arioch 'The Dec 24 '13 at 12:21
  • `ts := TMemoryStream.Create;` - where is `ts.Destroy` or `ts.Free` then ? // `mybitmap:= TBitmap.Create;` - where is `mybitmap.Destroy` or `mybitmap.Free` ? // why not simpler `Image1.Picture.bitmap.LoadFromStream(Ts);` ? – Arioch 'The Dec 24 '13 at 12:25
  • This might be a [Paradox compatibility issue](http://stackoverflow.com/a/20819805/205376) too. @user3051457, dont hesitate to answer your own question so other users might benefit from the solution. – Free Consulting Dec 28 '13 at 22:34
  • i tried same code with the delphi fire monkey form then its worked – Azad Jan 02 '14 at 10:23

0 Answers0