Is there any way I can load a image in to text file in Delphi [closed]
I have a Image and i want to upload this image in a text file.is there any way I can do it using Delphi. Consider the image is a Barcode image and i want this image to be there at a particular location in the text file. This text file will be开发者_运维知识库 then uploded to report viewer where it will be printed in a report format.
If the 'text file' is just an exchange format, you could encode the picture as hexadecimal or as Base64 encoding (which will uses less space).
You've got BinToHex and HexToBin functions in Classes unit.
For instance:
function SaveAsText(Bmp: TBitmap): AnsiString;
var MS: TMemoryStream;
begin
MS := TMemoryStream.Create;
try
Bmp.SaveToStream(MS);
SetLength(result,MS.Size*2);
BinToHex(MS.Memory,pointer(result),MS.Size);
finally
MS.Free;
end;
end;
procedure LoadFromText(Bmp: TBitmap; const Text: AnsiString);
var MS: TMemoryStream;
begin
MS := TMemoryStream.Create;
try
MS.Size := length(Text) shr 1;
HexToBin(pointer(Text),MS.Memory,MS.Size);
Bmp.LoadFromStream(MS);
finally
MS.Free;
end;
end;
This is the method used e.g. by the MIME format to attach a binary file to an email (using Base64 encoding). An email is just some text. You may have to add some delimiters before and after the hexa/base64 text, just as MIME, to mark that this is some data, not text.
精彩评论