A ver no estoy muy puesto en el tema, pero lo que quería explicar es más o menos esto. Seguro que es optimizable, ya digo que no estoy muy puesto, pero así creo que se entiende mejor:
(1) Poner un TImage en el form, con un bitmap.
(2) POner un memo con un texto.
(3) Ejecutar el código siguiente.
El resultado es un fichero en disco con:
10 dígitos correspondiente al tamaño del memo
10 dígitos correspondientes al tamaño de la imagen
El contenido del memo
El contenido de la imagen
Si lo abrís con un editor hexadecimal se ve perfectamente.
Si antes de guardar se le aplica un algoritmo y listo.
Con los dos tamaños (ocupan cada uno 10 posiciones) se debería poder hacer el paso contrario sin problemas.
Código Delphi
[-]
var
i:Integer;
s:Int64;
Str:String;
buff: array [0..9] of byte;
f1, F2: TFileStream;
s1:TMemoryStream;
buffmemo: array [0..4000] of byte;
sMemo:Integer;
begin
f1 := TFileStream.Create('.\I.dat', fmCreate);
f2 := TFileStream.Create('.\M.dat', fmCreate);
s1 := TMemoryStream.Create;
Memo1.Lines.SaveToStream(f2);
s := sizeof(f2);
for i := 0 to Min(4000, Length(Memo1.Lines.TExt)) do begin
buffmemo[i] := Ord(Memo1.Lines.Text[i + 1]);
end;
sMemo := i;
Str := DupeString('0', 10) + IntToStr(sMemo);
Str := Copy(Str, Length(Str) - 10 + 1, 10);
for i := 0 to 9 do begin
buff[i] := Ord(Str[i + 1]);
end;
f1.WriteBuffer(buff, 10);
Image1.Picture.Bitmap.SaveToStream(s1);
s := s1.Size;
Str := DupeString('0', 10) + IntToStr(s);
Str := Copy(Str, Length(Str) - 10 + 1, 10);
for i := 0 to 9 do begin
buff[i] := Ord(Str[i + 1]);
end;
f1.WriteBuffer(buff, 10);
f1.WriteBuffer(buffmemo, sMemo);
Image1.Picture.Bitmap.SaveToStream(f1);
FreeAndNil(s1);
FreeAndNil(f2);
FreeAndNil(f1);