将 BitMap 转换为没有换行符的字符串? [英] Convert BitMap to string without line breaks?
问题描述
我在某处找到了将 BitMap 转换为字符串的代码:
Somewhere I found this code to convert a BitMap to a string:
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;
然而,这会返回一个包裹线块.有没有可能得到一行而没有换行符?
However, this gives back a block of wrapped lines. Is it possible to get one single line without line breaks?
推荐答案
XE7 引入了 System.NetEncoding
包含 base64 编码器的单元.这旨在用于而不是来自 Soap.EncdDecd
的编码器.
XE7 introduces the System.NetEncoding
unit which contains a base64 encoder. This is intended to be used rather than the encoder from Soap.EncdDecd
.
TBase64Encoding
的构造函数 具有允许您指定每行字符(和换行文本)的重载.不幸的是,Embarcadero 还没有提供文档,所以我们需要阅读源代码来确定到底要通过什么.这样做之后,很明显只有当每行的字符数严格为正时才插入换行符.所以创建一个这样的编码对象:
The constructor of TBase64Encoding
has overloads that allow you to specify characters per line (and line break text). Unfortunately Embarcadero haven't deigned to provide documentation yet so we need to read the source to work out what exactly to pass. Having done that, it becomes clear that line breaks are inserted only if the number of characters per line is strictly positive. So create an encoding object like this:
Encoding := TBase64Encoding.Create(0);
一旦您有了合适的编码对象,请使用 Encode
将您的字节流编码为文本.和 Decode
来反转这个过程.
Once you have a suitable encoding object use Encode
to encode your stream of bytes to text. And Decode
to reverse the process.
可能看起来像这样:
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;
或者,您可以使用 EncodeBytesToString
和 DecodeStringToBytes
.可能看起来像这样:
Alternatively you can use EncodeBytesToString
and DecodeStringToBytes
. Which might look like this:
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
单元的一部分,但添加为类助手也很简单.
What would be really good would be if we could have something like the compression and decompression streams that are available with zlib. We could create an encoding stream, save the bitmap to it, and read out the base64 text. In the other direction we'd feed base64 text into a decoding stream, and then load the bitmap from it. This should really be part of the NetEncoding
unit, but it would be simple enough to add as a class helper.
这篇关于将 BitMap 转换为没有换行符的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!