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

前端 未结 2 408
独厮守ぢ
独厮守ぢ 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 13:59

    I realize this is a tad late, but I wanted to contribute. I quite simply did the following, having not to worry about the image format (.jpg, .png etc.). I simply create a TStream object, load the BLOB stream into it, and then I load the Image from the stream. Short and sweet, and it works great for me.

       var
         Stream : TStream;
       begin
         try
           Stream := TStream.Create;
           Stream := Dataset.CreateBlobStream(Dataset.FieldByName('SIGNATURE'), bmRead);
           Stream.Position := 0;
           lblPicSize.Caption := 'Picture is ' + IntToStr(Stream.Size) + ' Bytes';
           if Stream.Size <= 0 then
             pnlPic.Caption := '<No Signature>'
           else
             pnlPic.Caption := '';
    
           try
             imgSignature.Picture.LoadFromStream(Stream);
           except
             on E:Exception do
               begin
                 ShowMessage(E.Message);
               end;
           end;
    
     finally
       Stream.Free;
     end;
    end;
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题