How to display BLOB Image from database in the TAdvStringGrid with the help of DataSet

前端 未结 2 427
独厮守ぢ
独厮守ぢ 2020-12-18 13:32

I have been making an application at Delphi XE3. I am trying to display values from database to the TAdvStringGrid component placed on the form. I am using data

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 14:22

    CreateBlobStream is creating a TStream object, not a TMemoryStream.
    Since you do not want to write the JPG to the database you should use bmRead instead of bmReadWrite.
    I am not used to SQLite, but you will have to make sure that you are using a suitable binary datetype (BLOB).

      JPG := TJpegImage.Create;
      Picture:= TPicture.Create;
      try
        st := results.CreateBlobStream(TBlobField(results.FieldByName('image')), bmRead);
        try
          JPG.LoadFromStream(st);
          Picture.Assign(JPG);
          sg.AddPicture(i,j,Picture,True,ShrinkWithAspectRatio,0,haLeft,vaTop);
        finally
          st.Free;
        end;
      finally
        JPG.Free;
        Picture.Free;
      end;
    

    To ensure that the stored image is really a JPG you should write the JPG for testing with something like:

    var
      ms: TMemoryStream;
    begin
      ads.Open;
      ads.Append;
      ms := TMemoryStream.Create;
      try
        Image1.Picture.Graphic.SaveToStream(ms); // make sure having loaded a JPG
        ms.Position := 0;
        TBlobField(ads.FieldByName('image')).LoadFromStream(ms);
      finally
        ms.Free;
      end;
      ads.Post;
    end;
    

提交回复
热议问题