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

梦想与她 提交于 2019-11-29 08:42:46

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;
Hendrik Potgieter

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!