--- Provides an abstract data type TSTream 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 (Const Buffer; Count: long): longint; virtual; abstentract;
---- 3. Function seek (offset: longint; origin: word): long;
---- 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. SaveTostream (Stream: TSTREAM); // Write the data in the class into the current location of Stream
---- 2. LoadFromstream (Stream: TSTREAM); // Read the data in Stream from the current location
---- We basically use the above two functions when you use it.
Three, examples
---- TSTREAM inheritance tree diagram is shown in Figure 1 (omitted), which is commonly used in actual use is TFileStream, TMEMORYSTREAM, TBLOBSTREAM, in which the specific usage is explained 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 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 the
Begin
MS: = 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
M
S: tmemorystream;
PIMAGE: POINTER;
Begin
MS: = TMEMORYSTREAM.CREATE
Image1.picture.bitmap.savetostream (ms);
Ms.Position: = 0;
PIMAGE: = invert (ms.memory, ms.size);
// Memory property is a pointer to the actual memory block
Ms.write (PIMAGE>, MS.SIZE);
Ms.Position: = 0;
// The last line of code makes the pointer to the end of Stream, so it is necessary to reset
Image1.Picture.bitmap.LoadFromstream (MS);
FreeMem (PIMAGE);
Ms.free;
END;
The 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: = 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;
// Add a record in the database
TBLOBFIELD (Table1.fieldByname
('Image')). LoadFromstream (MS);
Table1.post;
// 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;
END;
---- Now you have been able to read and write data from files, databases and memory. Try it.