图片保存为字符串,字符串还原的图片 用内存流实现 ( 积分: 100 )

  • 主题发起人 主题发起人 jlugjb
  • 开始时间 开始时间
J

jlugjb

Unregistered / Unconfirmed
GUEST, unregistred user!
有以前的一个例子,但调试无法通过,谁能帮我把这段代码调试一下,实在不知道怎么解决了。

function BmpToString(Bmp: TBitmap): string;

var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
Bmp.SaveToStream(BinStream);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;
end;
finally
BinStream.Free
end;
end;

function StringToBmp(Value: string): TBitmap;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result.LoadFromStream(BinStream);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;
 
有以前的一个例子,但调试无法通过,谁能帮我把这段代码调试一下,实在不知道怎么解决了。

function BmpToString(Bmp: TBitmap): string;

var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
Bmp.SaveToStream(BinStream);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;
end;
finally
BinStream.Free
end;
end;

function StringToBmp(Value: string): TBitmap;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result.LoadFromStream(BinStream);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;
 
李维的《Delphi 6/Kylix 2 Soap/Web Services》
第8.3.1节 图形处理Web Service应用系统

在EncdDecd程序单元中有如下的函数可以让程序员调用来进行Base64编码和译码的工作:
procedure EncodeStream(Input,Output:TStream);
procedure DecodeStream(Input,Output:TStream);
function EncodeString(const Input:String):String;
function DecodeString(const Input:String):String;
示例如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,EncdDecd, ExtDlgs, StdCtrls, ExtCtrls,jpeg;
//要use EncdDecd和jpeg单元,因为此例中用的是.jpg图片
type
TForm1 = class(TForm)
Image1: TImage;
btnEncode: TButton;
btnDecode: TButton;
Memo1: TMemo;
OpenPictureDialog1: TOpenPictureDialog;
procedure btnEncodeClick(Sender: TObject);
procedure btnDecodeClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

function MakeBitmapIntoString(const sFileName:String):String;
var
ass:TStringStream;
ams:TMemoryStream;
begin
ams:=TMemoryStream.Create;
ass:=TStringStream.Create('');
try
ams.LoadFromFile(sFileName);
ams.SaveToStream(ass);
Result:=EncodeString(ass.DataString);
finally
ams.Free;
ass.Free;
end;
end;

procedure MakeStringIntoBitmap(const sBitmapString:String);
var
ass:TStringStream;
ams:TMemoryStream;
sNewFile:String;
begin
sNewFile:=ExtractFilePath(Application.ExeName)+'tt.jpg';
ass:=TStringStream.Create(DecodeString(sBitmapString));
ams:=TMemoryStream.Create;
try
ams.LoadFromStream(ass);
ams.SaveToFile(sNewFile);
finally
ass.Free;
ams.Free;
end;
end;

procedure TForm1.btnEncodeClick(Sender: TObject);
var
sNewFile:String;
begin
if OpenPictureDialog1.Execute then
//在Memo1中可看到转换后的字符串
Memo1.Text:=MakeBitmapIntoString(OpenPictureDialog1.FileName);
end;

procedure TForm1.btnDecodeClick(Sender: TObject);
begin
//把字符串转换为图片并保存到ExtractFilePath(Application.ExeName)+'tt.jpg'
MakeStringIntoBitmap(Memo1.Text);
Image1.Picture.LoadFromFile(sNewFile);
end;

end.
 
接受答案了.
 
后退
顶部