Delphi stream operation skills summary

zhaozj2021-02-17  49

Delphi stream operation skills summary

The inheritance relationship of TMemoryStream is as follows TOBJECT | TSTREAM | TCUSTomMemoryStream | TMEMORYSTREAM

How to use TMEMORYSTREAM? In fact, TMEMORYSTREAM uses the same specific properties as TSTREAM, the method can help. Rewinding an example: If I want to read and write a bitmap directly in memory, what should I do? TmemoryStream helps you are busy with Var BitmapMemoryStream: TmemoryStream; Bitmap1: Tbitmap;

procedure TForm.Button1Click (Sender: TObject); begin BitmapmemroyStream: = TmemoryStream.Create; file: // establish 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: // save memory stream, the size of file: //Bitmap1.bmp the same except showmessage; end ( 'error on access memory!') End; end; procedure tform.button4click (sender: TOBJECT); var buffer: array [0..53] of char; becomi assigned (BitmapMemroyStream) THEN TH Y BitmapMemRoyStream.seek (0, Sofrombeginning); BitmapMemoryStream.read (Buffer, 54); if Buffer [0] = 'b' and buffer [1] = 'm' Then File: // Remove memory content Begin BitmapMemoryStream.seek 0, SOFROMBEGINNING); BitmapMemoryStream.write ('Ice', 3); Button3Click (sender); // Written content written into file end; Except ShowMessage ('Error on AcroyStream'; End; End; everyone can see When I use TMEMORYSTREAM how convenient to read with memory, of course, it is not necessary to build a bitmap to directly boot file with loadFromFile, but if other memory flows are available in the above method, some other The function can be helpful, do yourself! There are still many other streaming objects, which are roughly almost, one through hundred!

How to write a stream in the clipboard and handle this technique is done with the implementation of the clipboard class of Delphi. Put a stream of content into a clipboard, first to register your own format, use the registerclipboardformat () function and do three steps below: 1. Create a content stream and write the content into 2. Create a global content area, And write the contents of the stream 3. Call clipboard.setashandle () Write the content to the clipboard

Write the content in the clipboard VAR HBUF: THANDLE; BUFPTR: POINTER; MSTREAM: TMEMORYSTREAM; begin mstream: = tmemorystream.create; try {- Processing code -} 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; Please note that the allocated global buffer is not released, this work is done by the clipboard, and you should copy it after reading the data.

The contents of the clipboard were read out from the contents of Var Hbuf: Thandle; Bufptr: Pointer; MSTREAM: TMEMORYSTREAM; Begin Hbuf: = clipboard.getashandle (CF_MYFORMAT); if HBUF <> 0 THEN BEGIN BUFPTR: = Globalock (HBUF); if bufptr <> nil then begin try mstream: = TMemoryStream.Create; try mstream.WriteBuffer (bufptr ^, GlobalSize (hbuf)); mstream.Position: = 0; {- processing the code stream -} finally mstream.Free; end; finally GlobalUnlock (hbuf); end; end; end;

Use TSTREAM to read and write data in DEPHI

A abstract data type TSTREAM is provided in DepHi to support the operation of streaming data. These data are usually from files, databases, memory objects, OLE objects, etc., TSTREAM provides unified, concise approach to read and write data. Under normal circumstances, we don't need to use the TSTREAM class directly, and the read and write packages of streaming data are in the method of VCL controls. But if these methods cannot meet our requirements, they need to manually control the reading and writing.

I. Common methods and properties of TSTREAM: ---- 1. Function Read (VAR Buffer; Count: long): longint; virtual; abstract ---- 2. Function Write (const buffer; count: longint): longint .

---- 3. Function seek (Offset: longint; Origin: Word): Longint; Virtual; Abstract; ---- 4. Property Position: longint

---- 5. Property Size: longint

---- Read, Write, seek is a pure virtual function, providing an abstract method of data reading and location. The READ method reads the data from the Stream to the buffer buffer. Write implements the opposite operation, the return value represents the size of the actual read and write data. Seek provides a way to move a data pointer in Stream. Parameters Origin can take SOFROMBEGINNING, SOFROMCURRENT, SOFRMEND three values, offset is offset, and the return value is the location of the current Stream data pointer.

---- Position represents the location of the data pointer in the stream. This attribute is readable. It is actually achieved by calling the Seek method, so it is more convenient to use this property when actually use. The size property represents the current STREAM size, for different stream, sometimes read-only.

Second, the reading and writing of Stream data. ---- 1. SaveTroupTream (Stream: TSTREAM); file: // Write the data in the class into the current location of Stream ---- 2. LoadFromstream (Stream: TSTREAM); file: // Read from the current location Into the data in Stream

---- We basically use the above two functions when you use it.

Third, the example ---- TSTREAM inheritance tree diagram is shown in Figure 1, which is often used in actual use of TFileStream, TMEMORYSTREAM, TBLOBSTREAM, in these three booming examples. Specific usage. ---- Create a form FORM1, place three buttons btnread, btninvert, btnsave, and a file Open dialog OpenDialog1 and data control DataSource1, Table1, Test.

---- Create a table test with DEPHI to create a table test, a field domain image, the database file name is Test.db. Place a TDATABASE control DBTEST on the form, a TTable control table1, a DataSource control Datasource1, a TDBNAVIGATOR control DBNAVIGATOR1. Connecting DBTest with the database created by Desktop, Table1's TableName property is set to test.db. The DataSource1's DataSet property is set to Table1. The Datasource property of DBNAVigator1 is set to DataSource1, and the VisibleButtons property is set to true. In addition, set the DBTEST's Connected to true, and the active property of Table1 is set to TRUE, so that the database is in an open state.

---- Event code is written as follows:

---- 1. BTNRead's Click event, which demonstrates the use of TFileStream.

var MS: TFileStream; begin if OpenDialog1.Execute then beginMS: = TFileStream.Create (OpenDialog1.FileName, fmOpenRead); Image1.Picture.Bitmap.LoadFromStream (MS); MS.Free; end; end;

---- 2. BTNINVERT Click event, which demonstrates the usage of TMemoryStream. The invert function is used, which is a simple function (which is valid only to the true color image only), which returns a pointer to the processed image data block. Var MS: TmemoryStream; PIMAGE: POINTER; begin ms: = tmemorystream.create; image1.picture.bitmap.savetostream (ms); ms.position: = 0; pimage: = invert (ms.memory, ms.size); file : // Memory property is a pointer (pimage ^, ms.size) that points to the actual memory block; ms.position: = 0; file: // The last row code makes the pointer to the end of Stream, so you want to reset image1. Picture.bitmap.LoadFromStream (ms); freeMem (pimage); ms.free; end; invert function is as follows: 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 1; End; Result: = PMEM; END;

--- 1. BTnsave Click event, which shows another usage of TMEMoryStream, writing data in Stream to the database. Var ms: tmemorystream; begin ms: = tmemorystream.create; image1.picture.bitmap.savetostream (ms); ms.position: = 0; table1.append; file: // Add a record in the database TBLOBFIELD (Table1.fieldbyname ('Image')). LoadFromStream (MS); table1.post; file: // Write the update to the database END;

---- 4. DBNAVigator1 Click event, which demonstrates TBLobStream usage, uses different methods of writing and writing to read image data for the database. Var MS: TSTREAM; Begin with Table1 Do MS: = CreateBlobStream (FieldByName ('Image'), BMREAD; Image1.Picture.bitmap.LoadFromstream (MS); Ms.Free;

After full text, thank you for reading this article. I lack available points, I have to earn points to participate in the exchange. If you feel this article, please vote for you, thank you.

ruthless

转载请注明原文地址:https://www.9cbs.com/read-29275.html

New Post(0)