Board index » delphi » Saving a TImage to a Textfile

Saving a TImage to a Textfile

Is there a way to save a TImage to file as text?

I thought about using a TMemoryStream to write to disk but that is a binary
file.  As all of my images are 48x48 256 colors bitmaps. I thought I might
be able to create an array of 48x48 of byte and store just the numerical
values as text, where the value represents the color for that pixel.

I'm not sure how I would recreate the image from the 48x48 array once I read
it in from the file.

Thanks for the help.

Tony

 

Re:Saving a TImage to a Textfile


On Thu, 27 Sep 2001 10:36:30 -0500, "Tfrey" <tf...@del.ourempire.com>
wrote:

Quote
>Is there a way to save a TImage to file as text?

Run it through Base64 algorithm first.  

Nick Hodges - TeamB
HardThink, Inc.
Please always follow the newsgroup guidelines --
http://www.borland.com/newsgroups

Re:Saving a TImage to a Textfile


Quote
"Tfrey" <tf...@del.ourempire.com> wrote in message news:3bb34781_2@dnews...
> Is there a way to save a TImage to file as text?

I use a function for converting a binary stream into a string, and a
procedure for converting the string back to a binary stream.

Here is what I have done and have used without any problems:

{---------------------------------------------------------------------------
-------------}
Function sStreamToHexString(aStream: TStream): String;
Var
  lCurrentLocation    : LongInt;
  lpszStr             : PChar;
  lNumOfCharacters    : LongInt;
  TmpStream           : TMemoryStream;
Begin
  lCurrentLocation:= aStream.Seek(0, soFromCurrent);
  aStream.Seek(0, soFromBeginning);

  TmpStream:= TMemoryStream.Create;
  TmpStream.LoadFromStream(aStream);
  TmpStream.Seek(0, soFromBeginning);

  lNumOfCharacters:= TmpStream.Size * 2;
  lpszStr:= StrAlloc(lNumOfCharacters + 16);
  FillChar(lpszStr^, StrBufSize(lpszStr) - 1, #0);

  BinToHex(TmpStream.Memory, lpszStr, TmpStream.Size);

  Result:= StrPas(lpszStr);
  StrDispose(lpszStr);
  TmpStream.Free;

  aStream.Seek(lCurrentLocation, soFromBeginning);
End;
{---------------------------------------------------------------------------
-------------}
Procedure HexStringToStream(aHexString: String; aStream: TStream);
Var
  lNumOfCharacters    : LongInt;
  TmpStream           : TMemoryStream;
Begin
  If(Length(aHexString) > 0) Then
  Begin
    lNumOfCharacters:= Length(aHexString) Div 2;

    TmpStream:= TMemoryStream.Create;
    TmpStream.SetSize(lNumOfCharacters);
    TmpStream.Seek(0, soFromBeginning);

    HexToBin(PChar(aHexString), TmpStream.Memory, (lNumOfCharacters * 2));

    TmpStream.Seek(0, soFromBeginning);
    TmpStream.SaveToStream(aStream);
    aStream.Seek(0, soFromBeginning);
    TmpStream.Free;
  End;
End;

I hope this helps,

Farshad R. Vossoughi

Re:Saving a TImage to a Textfile


Hi,

For me, easiest way is copy that image into some canvas, and directly read
from canvas.pixels property.
Like following:

procedure TForm1.Button1Click(Sender: TObject);
begin
 Image1.Picture.LoadFromFile('c:\1.bmp');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
 i,j:Integer;
begin
 for i:=1 to Image1.Picture.Width do
   for j:=1 to Image1.Picture.Height do
    Canvas.Pixels[i,j]:=Image1.Canvas.Pixels[i,j];
end;

That is very easy. When you clicking Button1, app will load a picture to the
TImage. If you click Button2, then it copies all of the pixels of TImage to
the Form.
You have to change Button2Click.

Good luck :-)

Other Threads