TMemoryStream的繼承關(guān)系如下
TObject
|
TStream
|
TCustomMemoryStream
|
TMemoryStream
如何使用TMemoryStream?
其實(shí)TmemoryStream使用就跟TStream 一樣
具體的屬性,方法可看幫助。
下舉一例:
如我想在內(nèi)存中直接讀寫(xiě)一個(gè)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://內(nèi)存流保存,大小與
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://改寫(xiě)內(nèi)存內(nèi)容
begin
BitmapMemoryStream.Seek(0,soFromBeginning);
BitmapmemoryStream.Write('ICE',3);
Button3Click(Sender);//將改寫(xiě)的內(nèi)容寫(xiě)入文件
end;
except
ShowMessage('error On Access memroyStream');
end;
end;
大家可看到用TMemoryStream對(duì)與內(nèi)存讀寫(xiě)多么方便,當(dāng)然其實(shí)用不著先建一Bitmap
可以用LoadFromFile直接引導(dǎo)文件,但是如果對(duì)于其它的內(nèi)存流卻是可以用上述方法
上文只是拋轉(zhuǎn)引玉,其它的一些功能大家可以看幫助,自己琢磨!
還有很多其它的流式對(duì)象,大致都差不多,一通百通!
如何將一個(gè)流的內(nèi)容寫(xiě)入到剪貼板中,并處理
這個(gè)技巧是參考Delphi的剪貼板類(lèi)的實(shí)現(xiàn)來(lái)完成的。將一個(gè)流的內(nèi)容放入剪貼板,
首先要注冊(cè)你自已的格式,使用RegisterClipboardFormat()函數(shù)
然后做下面三步:
1.創(chuàng)建一個(gè)內(nèi)容流,并將內(nèi)容寫(xiě)進(jìn)去
2.創(chuàng)建一個(gè)全局的內(nèi)容區(qū),并將流的內(nèi)容寫(xiě)入
3.調(diào)用ClipBoard.SetAsHandle()將內(nèi)容寫(xiě)入剪貼板
將內(nèi)容寫(xiě)入剪貼板中
var
hbuf : THandle;
bufptr : Pointer;
mstream : TMemoryStream;
begin
mstream := TMemoryStream.Create;
try
{-- 處理流的代碼 --}
hbuf := GlobalAlloc(GMEM_MOVEABLE, mstream.size);
try
bufptr := GlobalLock(hbuf);
try
Move(mstream.Memory^, bufptr^, mstream.size);
Clipboard.SetAsHandle(CF_MYFORMAT, hbuf);
finally
GlobalUnlock(hbuf);
end;
except
GlobalFree(hbuf);
raise;
end;
finally
mstream.Free;
end;
end;
請(qǐng)注意不要將分配的全局緩沖區(qū)釋放,這個(gè)工作由剪貼板來(lái)完成,在讀出數(shù)據(jù)中
你應(yīng)該將它復(fù)制后處理。
將剪貼板內(nèi)容讀出來(lái)
var
hbuf : THandle;
bufptr : Pointer;
mstream : TMemoryStream;
begin
hbuf := Clipboard.GetAsHandle(CF_MYFORMAT);
if hbuf <> 0 then begin
bufptr := GlobalLock(hbuf);
if bufptr <> nil then begin
try
mstream := TMemoryStream.Create;
try
mstream.WriteBuffer(bufptr^, GlobalSize(hbuf));
mstream.Position := 0;
{-- 處理流的代碼 --}
finally
mstream.Free;
end;
finally
GlobalUnlock(hbuf);
end;
end;
end;
end;
在Dephi中使用TStream讀寫(xiě)數(shù)據(jù)的技巧
在Dephi中提供了一個(gè)抽象的數(shù)據(jù)類(lèi)型TStream來(lái)支持對(duì)流式數(shù)據(jù)的操作。這些數(shù)據(jù)通常來(lái)自文件、數(shù)據(jù)庫(kù)、內(nèi)存對(duì)象、OLE對(duì)象等,TStream提供了統(tǒng)一、簡(jiǎn)潔的方法來(lái)進(jìn)行數(shù)據(jù)的讀寫(xiě)。在通常情況下,我們并不需要直接使用TStream類(lèi),對(duì)流式數(shù)據(jù)的讀寫(xiě)封裝在VCL控件的方法中。但是如果這些方法無(wú)法滿(mǎn)足我們的要求,就需要自己手動(dòng)控制數(shù)據(jù)的讀寫(xiě)。
一、 TStream的常用的方法和屬性:
---- 1. function Read(var Buffer; Count: Longint): Longint; virtual; abstract
---- 2. function Write(const Buffer; Count: Longint): Longint; virtual; abstract;
---- 3. function Seek(Offset: Longint; Origin: Word): Longint; virtual; abstract;
---- 4. property Position: Longint;
---- 5. property Size: Longint
---- Read,Write,Seek都是純虛函數(shù),提供了數(shù)據(jù)讀寫(xiě)和定位的抽象的方法。Read方法將數(shù)據(jù)從Stream中讀到Buffer緩沖區(qū)中,Write則實(shí)現(xiàn)相反的操作,返回值表示實(shí)際讀寫(xiě)數(shù)據(jù)的大小。Seek提供了在Stream中移動(dòng)數(shù)據(jù)指針的方法。參數(shù)Origin可以取soFromBeginning,soFromCurrent,soFromEnd 三個(gè)值,Offset是偏移量,返回值是當(dāng)前Stream數(shù)據(jù)指針的位置。
---- Position表示了數(shù)據(jù)指針在Stream中的位置。這個(gè)屬性是可讀寫(xiě)的,它實(shí)際上就是通過(guò)調(diào)用Seek方法實(shí)現(xiàn)的,所以實(shí)際使用時(shí)使用這個(gè)屬性更為方便一些。Size屬性表示當(dāng)前Stream的大小,對(duì)于不同的Stream,有些時(shí)候是只讀的。
二、 Stream數(shù)據(jù)的讀寫(xiě)。
---- 1. SaveToStream(Stream: TStream ); file://將類(lèi)中的數(shù)據(jù)寫(xiě)到Stream的當(dāng)前位置中
---- 2. LoadFromStream(Stream: TStream); file://從當(dāng)前位置讀入Stream里的數(shù)據(jù)
---- 實(shí)際使用時(shí)我們基本上只要使用上面兩個(gè)函數(shù)就可以了。
三、 例子
---- TStream的繼承樹(shù)圖如圖1所示(略),實(shí)際使用時(shí)比較常用的是TFileStream,TMemoryStream,TblobStream,就以這三種流舉一例說(shuō)明具體用法。
---- 創(chuàng)建一個(gè)窗體Form1,放置三個(gè)按鈕btnRead,btnInvert,btnSave和一個(gè)文件打開(kāi)對(duì)話(huà)框OpenDialog1以及數(shù)據(jù)控件DataSource1,Table1,test.
---- 使用Dephi提供的Database Desktop創(chuàng)建一個(gè)表test,表里有一個(gè)字段域Image,數(shù)據(jù)庫(kù)文件名存為test.db。在窗體上放置一個(gè)TDatabase控件dbTest,一個(gè)TTable控件Table1,一個(gè)DataSource控件DataSource1,一個(gè)TDBNavigator控件DBNavigator1。將dbTest與剛才Desktop創(chuàng)建的數(shù)據(jù)庫(kù)相連,Table1的TableName屬性設(shè)為test.db,DataSource1的DataSet屬性設(shè)為T(mén)able1,DBNavigator1的DataSource屬性設(shè)為DataSource1,VisibleButtons屬性前四個(gè)設(shè)為T(mén)RUE。此外,將dbtest的Connected設(shè)為T(mén)RUE,Table1的Active屬性設(shè)為T(mén)RUE,使得數(shù)據(jù)庫(kù)一開(kāi)始就處于打開(kāi)狀態(tài)。
---- 事件代碼編寫(xiě)如下:
---- 1. btnRead的Click事件,這里演示了TFileStream的用法。
var
MS: TFileStream;
begin
if OpenDialog1.Execute then
begin
MS:=TFileStream.Create
(OpenDialog1.FileName, fmOpenRead);
Image1.Picture.Bitmap.LoadFromStream(MS);
MS.Free;
end;
end;
---- 2. btnInvert的Click事件,這里演示了TMemoryStream的用法。其中使用了Invert函數(shù),這是一個(gè)簡(jiǎn)單的將圖象反色的函數(shù)(僅對(duì)真彩圖象有效),它返回一個(gè)指向處理過(guò)的圖象數(shù)據(jù)塊的指針。
var
M
S: TMemoryStream;
pImage: pointer;
begin
MS:=TMemoryStream.create;
Image1.Picture.Bitmap.SaveToStream(MS);
MS.Position:=0;
pImage:=Invert(MS.Memory, MS.size);
file://Memory屬性是指向?qū)嶋H內(nèi)存塊的指針
MS.Write(pImage^,MS.size);
MS.Position:=0;
file://上一行代碼使指針移到了Stream末尾,所以要復(fù)位
Image1.Picture.Bitmap.LoadFromStream(MS);
FreeMem(pImage);
MS.Free;
end;
Invert函數(shù)如下:
function TForm1.Invert
(pImage: pointer; size: Integer): pointer;
var
pData, pMem: PChar;
i: Integer;
begin
pMem:=AllocMem(size);
CopyMemory(pMem,pImage,size);
pData:=pMem+54;
for i:=0 to size-54-1 do
begin
pData^:=Char(not integer(pData^));
pData:=pData+1;
end;
Result:=pMem;
end;
---- 1. btnSave的Click事件,這里演示了TMemoryStream的另一種用法,將Stream中的數(shù)據(jù)寫(xiě)到數(shù)據(jù)庫(kù)中去。
var
MS: TMemoryStream;
begin
MS:=TMemoryStream.create;
Image1.Picture.Bitmap.SaveToStream(MS);
MS.Position:=0;
Table1.Append;
file://在數(shù)據(jù)庫(kù)中添加一條記錄
TBlobField(Table1.FieldbyName
('image')).LoadFromStream(MS);
Table1.Post;
file://將所作的更新寫(xiě)入數(shù)據(jù)庫(kù)
end;
---- 4. DBNavigator1的Click事件,這里演示了TBlobStream的用法,使用了和寫(xiě)入時(shí)不同的方法來(lái)讀出數(shù)據(jù)庫(kù)的圖象數(shù)據(jù)。
var
MS: TStream;
begin
with Table1 do
MS:=CreateBlobStream
(FieldbyName('image'),bmRead);
Image1.Picture.Bitmap.
LoadFromStream(MS);
MS.Free;
end;
全文完,感謝你閱讀了這篇文章。本人缺乏可用分了,只好來(lái)掙點(diǎn)參與分好兌換。如感覺(jué)此文對(duì)你有幫助的朋友請(qǐng)投我一票,謝謝。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注