Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: Godfather on August 16, 2017, 10:34:54 am

Title: how to write picture to stream?
Post by: Godfather 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.


Title: Re: how to write picture to stream?
Post by: howardpc on August 16, 2017, 10:48:19 am
What is wrong with
Code: Pascal  [Select][+][-]
  1. Image1.Picture.LoadFromFile(PictFilename);
that you avoid it?
Title: Re: how to write picture to stream?
Post by: Thaddy 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.  

 ::)
Title: Re: how to write picture to stream?
Post by: Godfather 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.
Title: Re: how to write picture to stream?
Post by: J-G 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 ??   %)
Title: Re: how to write picture to stream?
Post by: howardpc 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.
Title: Re: how to write picture to stream?
Post by: balazsszekely 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;
Title: Re: how to write picture to stream?
Post by: Remy Lebeau 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.
Title: Re: how to write picture to stream?
Post by: Godfather on August 23, 2017, 08:16:35 am
Thank you, GetMem!
TinyPortal © 2005-2018