将BitMap转换为字符串而不换行?

时间:2015-03-03 00:24:02

标签: delphi bitmap encode delphi-xe7

我发现这个代码将BitMap转换为字符串:

function Base64FromBitmap(Bitmap: TBitmap): string;
var
  Input: TBytesStream;
  Output: TStringStream;
begin
  Input := TBytesStream.Create;
  try
    Bitmap.SaveToStream(Input);
    Input.Position := 0;
    Output := TStringStream.Create('', TEncoding.ASCII);
    try
      Soap.EncdDecd.EncodeStream(Input, Output);
      Result := Output.DataString;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

然而,这会给回一块包裹线。是否可以获得一条没有换行符的单行?

2 个答案:

答案 0 :(得分:5)

Soap.EncdDecd.EncodeStream()是硬编码的,每75个字符输出一次换行符。更改它的唯一方法是复制Soap.EncdDecd.pas,编辑它,然后将副本添加到项目中(如果您的项目启用了运行时包,则此方法不起作用。)

您可以使用Indy的TIdEncoderMIME课程替代。它不输出换行符:

uses
  ..., IdCoderMIME;

function Base64FromBitmap(Bitmap: TBitmap): string;
var
  Input: TMemoryStream;
begin
  Input := TMemoryStream.Create;
  try
    Bitmap.SaveToStream(Input);
    Input.Position := 0;
    Result := TIdEncoderMIME.EncodeStream(Input);
  finally
    Input.Free;
  end;
end;

答案 1 :(得分:3)

XE7引入了包含base64编码器的System.NetEncoding单元。这是为了使用而不是Soap.EncdDecd的编码器。

constructor of TBase64Encoding有重载,允许您指定每行字符(和换行符文本)。不幸的是,Embarcadero尚未设计提供文档,因此我们需要阅读源代码以确定准确传递的内容。完成后,很明显只有当每行的字符数严格为正时才会插入换行符。因此,创建一个这样的编码对象:

Encoding := TBase64Encoding.Create(0);

获得合适的编码对象后,使用Encode将字节流编码为文本。并Decode来扭转这一过程。

这可能是这样的:

function Base64FromBitmap(Bitmap: TBitmap): string;
var
  Input: TBytesStream;
  Output: TStringStream;
  Encoding: TBase64Encoding;
begin
  Input := TBytesStream.Create;
  try
    Bitmap.SaveToStream(Input);
    Input.Position := 0;
    Output := TStringStream.Create('', TEncoding.ASCII);
    try
      Encoding := TBase64Encoding.Create(0);
      try
        Encoding.Encode(Input, Output);
        Result := Output.DataString;
      finally
        Encoding.Free;
      end;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

procedure BitmapFromBase64(Base64: string; Bitmap: TBitmap);
var
  Input: TStringStream;
  Output: TBytesStream;
  Encoding: TBase64Encoding;
begin
  Input := TStringStream.Create(Base64, TEncoding.ASCII);
  try
    Output := TBytesStream.Create;
    try
      Encoding := TBase64Encoding.Create(0);
      try
        Encoding.Decode(Input, Output);
        Output.Position := 0;
        Bitmap.LoadFromStream(Output);
      finally
        Encoding.Free;
      end;
    finally
      Output.Free;
    end;
  finally
    Input.Free;
  end;
end;

或者,您可以使用EncodeBytesToStringDecodeStringToBytes。这可能是这样的:

function Base64FromBitmap(Bitmap: TBitmap): string;
var
  Stream: TBytesStream;
  Encoding: TBase64Encoding;
begin
  Stream := TBytesStream.Create;
  try
    Bitmap.SaveToStream(Stream);
    Encoding := TBase64Encoding.Create(0);
    try
      Result := Encoding.EncodeBytesToString(Copy(Stream.Bytes, 0, Stream.Size));
    finally
      Encoding.Free;
    end;
  finally
    Stream.Free;
  end;
end;

procedure BitmapFromBase64(Base64: string; Bitmap: TBitmap);
var
  Stream: TBytesStream;
  Bytes: TBytes;
  Encoding: TBase64Encoding;
begin
  Stream := TBytesStream.Create;
  try
    Encoding := TBase64Encoding.Create(0);
    try
      Bytes := Encoding.DecodeStringToBytes(Base64);
      Stream.WriteData(Bytes, Length(Bytes));
      Stream.Position := 0;
      Bitmap.LoadFromStream(Stream);
    finally
      Encoding.Free;
    end;
  finally
    Stream.Free;
  end;
end;

如果我们能够拥有zlib提供的压缩和解压缩流,那将会是非常好的。我们可以创建一个编码流,将位图保存到它,然后读出base64文本。在另一个方向,我们将base64文本提供给解码流,然后从中加载位图。这应该是NetEncoding单元的一部分,但是添加为类助手就足够了。