TMemoryStream的继承关系如下
TObject
|
TStream
|
TCustomMemoryStream
|
TMemoryStream
如何使用TMemoryStream?
其实TmemoryStream使用就跟TStream 一样具体的属性,方法可看帮助。
下举一例:
如我想在内存中直接读写一个Bitmap,怎么办?
TmemoryStream帮了你大忙
var
BitmapMemoryStream:TMemoryStream;
Bitmap1:TBitmap;
procedure TForm.Button1Click(Sender:TObject);
begin
BitmapmemroyStream:=TmemoryStream.Create; file://建立MemoryStream
Bitmap1:=TBitmap.Create;
try
Bitmap1.LoadFromFile(\'d:\\Bitmap1.bmp\');
except
ShowMessage(\'Error On LoadFile bitmap1.bmp\');
end;
end;
procedure TForm.Button2Click(Sneder:Tobject);
begin
if Assigned(Bitmap1) then
Bitmap1.SaveToStream(BitmapmemoryStream);
end;
procedure TForm.Button3Click(Sender:TObject);
begin
if BitmapMemoryStream<>nil then
begin
try
BitmapMemroyStream.SaveToFile(\'Bitmap1.str\'); file://内存流保存,大小与file://Bitmap1.bmp一样
except
showmessage(\'error on access memory!\');
end;
end;
end;
procedure TForm.Button4Click(Sender:TObject);
var
Buffer:Array[0..53] of char;
begin
if Assigned( BitmapMemroyStream) then
try
BitmapMemroyStream.Seek(0,soFromBeginning);
BitmapMemoryStream.Read(Buffer,54);
if Buffer[0]=\'B\' and Buffer[1]=\'M\' then file://改写内存内容
begin
BitmapMemoryStream.Seek(0,soFromBeginning);
BitmapmemoryStream.Write(\'ICE\',3);
Button3Click(Sender);//将改写的内容写入文件
end;
except
ShowMessage(\'error On Access memroyStream\');
end;
end;
大家可看到用TMemoryStream对与内存读写多么方便,当然其实用不着先建一Bitmap,可以用LoadFromFile直接引导文件,但是如果对于其它的内存流却是可以用上述方法
上文只是抛转引玉,其它的一些功能大家可以看帮助,自己琢磨!
还有很多其它的流式对象,大致都差不多,一通百通!
责任编辑:小草