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: longint): longint; virtual; abstract
2. Function WRITE (COUNGINT): Longint; Virtual; ABSTRACT
3. Function seek (offset: longint; origin: word): longint; virtual; abstentract;
4. Property Position: longint;
5. Property Size: Longint
READ, WRITE, SEEK are all pure virtual functions, providing an abstract method for reading and writing and positioning. 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 data in the class into the current location of Stream
2. LoadFromstream (Stream: TSTREAM); file: // Read the data in Stream from the current location
When we actually use, we can basically use the two functions above.
Third, the inheritance tree map of TSTREAM is shown in Figure 1 (omitted), which is commonly used in actual use is TFileStream, TMEMORYSTREAM, TBLOBSTREAM, in order to explain the specific usage in these three balances.
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 using 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. The 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. Here, it 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;
The invert function is as follows: Pimage: PIME: PME; 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's Click event, here demonstrates another Usage of TMEMoryStream, writing data in the 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 of the database END; 4. DBNAVIGATOR1 Click event, here demonstrate TBLOBSTREAM usage, use and write Method to read image data of the database. Var MS: TSTREAM; Begin with Table1 Do MS: = CreateBlobStream (FieldByName ('Image'), BMREAD; Image1.Picture.bitmap.LoadFromstream (MS); Ms.Free;