Recent

Author Topic: Blobs and TPicture  (Read 3586 times)

TRNelson

  • Jr. Member
  • **
  • Posts: 64
Blobs and TPicture
« on: March 26, 2016, 04:32:37 pm »
I have a small snippet of code that loads a picture file and stores it in a Blob in a table.

Code: Pascal  [Select]
  1. SQLQuery1.SQL.Text:= 'insert into Photos (PhotoID, FileName, Content) values (:PhotoID, :FileName, :Content)';
  2. SQLQuery1.ParamByName('PhotoId').AsInteger   := cntr;
  3. SQLQuery1.ParamByName('FileName').AsString   := Info.Name;
  4. SQLQuery1.ParamByName('Content').LoadFromFile(FName, ftBlob);
  5. SQLQuery1.ExecSQL;
  6. SQLTransaction1.Commit;

It works well and does the job. 

I would like to change the code so that the picture data comes from a TPicture object instead.  I could write the TPicture to a temporary file and reuse the code but that is pretty inefficient and I would rather extract the data directly from TPicture without having to resort to an intermediate file.

I can't find an elegant way of doing this.  All suggestions are welcomed!   :)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8114
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Blobs and TPicture
« Reply #1 on: March 26, 2016, 05:04:34 pm »
There's no direct way as far as I can find. You can use memory stream as intermediate storage, at least it won't suffer from disk I/O latency.

TRNelson

  • Jr. Member
  • **
  • Posts: 64
Re: Blobs and TPicture
« Reply #2 on: March 26, 2016, 05:47:11 pm »
There's no direct way as far as I can find. You can use memory stream as intermediate storage, at least it won't suffer from disk I/O latency.
That's what I thought as well.  The picture is already loaded in a TPicture.  Time to do some research on memory streams.

TRNelson

  • Jr. Member
  • **
  • Posts: 64
Re: Blobs and TPicture
« Reply #3 on: March 26, 2016, 06:30:11 pm »
Well, that turned out to be a lot easier than I thought it would be.  8)
Code: Pascal  [Select]
  1. Thumbnail.SaveToStream(MyStream);
  2. MyStream.Position := 0;
  3. Image1.Picture.LoadFromStream(MyStream);
I am continually surprised on how feature rich this system is.  It is a far more sophisticated than what I used to use 40 years ago. Fortran and S/360 Assembly language on punch cards.
 :)

Zath

  • Sr. Member
  • ****
  • Posts: 330
Re: Blobs and TPicture
« Reply #4 on: April 11, 2016, 09:42:08 pm »
Is position the actual physical position of an image file in a directory ?

howardpc

  • Hero Member
  • *****
  • Posts: 3201
Re: Blobs and TPicture
« Reply #5 on: April 11, 2016, 11:21:33 pm »
No, Position is a stream property, a pointer to a location somewhere within the entire length of the stream (for streams where Length makes sense) where the next stream read/write will take place.
To load a picture you want to begin at Position zero, right at the beginning of the stream (otherwise you would fail to read the initial bytes).

Zath

  • Sr. Member
  • ****
  • Posts: 330
Re: Blobs and TPicture
« Reply #6 on: April 12, 2016, 02:07:28 am »
Thank you howard.