Recent

Author Topic: how to write picture to stream?  (Read 5187 times)

Godfather

  • Newbie
  • Posts: 3
how to write picture to stream?
« on: August 16, 2017, 10:34:54 am »
i need a little help. My picture is already saved in bytes in a file. I want to show it with the help of any sort of Stream( Stream, Memorystream ... ),

this is how I save my picture to file.

    AssignFile( SourceFile, PictFilename );  // Picture
    Reset( SourceFIle );                              // Open picture 

    SizeOfPict := FileSize( SourceFile );       

    Write( MyFile, SizeOfPict SHR 24 );       // save the size of the picture to the file
    Write( MyFile, SizeOfPict SHR 16 );
    Write( MyFile, SizeOfPict SHR  8 );
    Write( MyFile, SizeOfPict SHR  0 );

    for cntr := 1 to SizeOfPict do begin

     read( SourceFIle, TempByte );                  // read picture in bytes
     write( MyFile, TempByte );                        // write picture in bytes to file

    end;

And now I would like some help with how to show the picture in TImage.

Thank you for your help.



howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: how to write picture to stream?
« Reply #1 on: August 16, 2017, 10:48:19 am »
What is wrong with
Code: Pascal  [Select][+][-]
  1. Image1.Picture.LoadFromFile(PictFilename);
that you avoid it?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: how to write picture to stream?
« Reply #2 on: August 16, 2017, 10:53:56 am »
Quote
this is how I save my picture to file.

    AssignFile( SourceFile, PictFilename );  // Picture
    Reset( SourceFIle );                              // Open picture 

Then use streams.... OMG....
Code: Pascal  [Select][+][-]
  1. .... oh well, Handoko will do this...
  2. begin
  3. end.
  4.  

 ::)
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Godfather

  • Newbie
  • Posts: 3
Re: how to write picture to stream?
« Reply #3 on: August 16, 2017, 11:12:18 am »
What is wrong with
Code: Pascal  [Select][+][-]
  1. Image1.Picture.LoadFromFile(PictFilename);
that you avoid it?

PictFilename is a string, but my picture is saved in bytes in the file! The thing is that in the file is more data, not only the picture but a memo text and other stuff, and everything is saved in BYTES, so its harder to get out the picture from that much data and open it, so the loadfromfile doesnt work, I tried that. That was the first thing I tried.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: how to write picture to stream?
« Reply #4 on: August 16, 2017, 12:42:54 pm »
PictFilename is a string, but my picture is saved in bytes in the file! The thing is that in the file is more data, not only the picture but a memo text and other stuff

WHY ??   %)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: how to write picture to stream?
« Reply #5 on: August 16, 2017, 02:30:17 pm »
PictFilename is a string, but my picture is saved in bytes in the file! The thing is that in the file is more data, not only the picture but a memo text and other stuff,

If you are not comfortable with using streams to save and load varied data types to and from files, it would be much easier for you to keep to standard file formats and have spearate files for the picture, memo.txt (Memo.Lines.LoadFromfile/SaveToFile) and other stuff (file of record routines?). Read three files, and save three files. It will save you a lot of grief compared with trying to save and subsequently read varying data types turned into a single stream of bytes representing composite data arbitrarily strung together in a single file.

balazsszekely

  • Guest
Re: how to write picture to stream?
« Reply #6 on: August 16, 2017, 03:00:38 pm »
What is TempByte? I assume TByte(array of Byte). Then you can do something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   TempByte: TBytes;
  3.   Bs: TBytesStream;
  4. begin
  5.   //...  
  6.   // read TempByte from file    
  7.   Bs := TBytesStream.Create(TempByte);
  8.   try
  9.     Bs.Position := 0;
  10.     Image1.Picture.LoadFromStream(Bs);
  11.   finally
  12.     Bs.Free;
  13.   end;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: how to write picture to stream?
« Reply #7 on: August 17, 2017, 08:20:19 pm »
PictFilename is a string, but my picture is saved in bytes in the file! The thing is that in the file is more data, not only the picture but a memo text and other stuff, and everything is saved in BYTES, so its harder to get out the picture from that much data and open it, so the loadfromfile doesnt work, I tried that. That was the first thing I tried.

TPicture has LoadFromStream() and LoadFromStreamWithFileExt() methods.

Since your data file knows where the picture data is located and how many bytes belong to the picture, you can either:

1. extract just those bytes from the file to a TMemoryStream or TByteStream, and then load that stream into TImage.

2. use a wrapper TStream that reads from the original file, limiting its reading to just the offsets of the picture data, and then load that wrapper stream into TImage.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Godfather

  • Newbie
  • Posts: 3
Re: how to write picture to stream?
« Reply #8 on: August 23, 2017, 08:16:35 am »
Thank you, GetMem!

 

TinyPortal © 2005-2018