Is there any way I can load a image in to text file in Delphi [closed]

前提是你 提交于 2020-01-14 06:04:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5313639/is-there-any-way-i-can-load-a-image-in-to-text-file-in-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!