Programs need to read and write data. Sometimes the data storage is only used temporarily. If the storage medium is a file, temporary files need to be deleted. Some Windows APIs allow reading and writing data to an object that implements IStream. IStream has methods that read and write to the underlying storage, which can be a file, memory, or even a named pipe: whatever the object chooses. However, IStream doesn’t inherit from IDispatch, so you can’t use Automation directly. Instead, I’ve created a simple COM wrapper that can read/write a stream, but uses the Library Construction Kit APIs to read and write Fox variables and fields.
Using memory for the stream instead of a disk file can be much faster, and releasing the stream can free the used memory.
The code below subclasses the GDIPlus wrapper foundation classes (which wraps the GDI+ Flat API ) and adds a new method that calls GdipSaveImageToStream in the GDI+ Flat API which will write to a stream. It loads a jpg into GDIPlus, writes some text and a timestamp on it, then saves it to a stream. The stream is then written to a VFP blob field and displayed on a form.
CreateStreamOnHGlobal is used to create a stream which uses memory as the storage.
Download the COM server (save it somewhere on your machine and regsvr32 it) here.
Let me know if you do any performance comparisons between file and stream I/O, or if you have other ideas on how to use streams with VFP.
See also: Draw text or graphics directly onto a JPG file
Create thumbnails of all your digital photos in a single table
Use Named Pipes to communicate between processes or machines
SET SAFETY OFF
SET CLASSLIB TO HOME()+"ffc\_gdiplus"
cFile=LOCFILE("d:\kids.jpg","jpg","JPG file") && Some jpg file
DECLARE integer CreateStreamOnHGlobal IN ole32 integer hGlobal,integer fDeleteOnRelease, integer @ nSrm
nStm=0 && the stream is represented as an integer. It's actually the IStream pointer, which does not inherit from IDispatch
*some test code to create, write, then read a stream
LOCAL ox as "VFPStream.Cstream"
CreateStreamOnHGlobal(0,1,@nStm) && Create stream nStm
sFileContents=FILETOSTR(cFile) && A VFP Variable which has the file contents
?"Write",ox.WriteStream(nStm,"sFileContents",nLen) && Write the contents to the stream
*ox.StreamSeek(nStm,0,0,0,0,0) && Seek to beginning of stream
buf=0 && Create a VFP Variable
?"Read",ox.ReadStream(nStm,"buf",0) && Read the stream contents into var "buf". nBytesToRead=0 means all of it
?"Stream length=",ox.GetStreamLen(nStm) && we can get the length
LOCAL oImage as gpimage OF HOME()+"ffc\_gdiplus",oThumb as gpimage
LOCAL oGraphics as gpGraphics
LOCAL orect as gprectangle
oImage=CREATEOBJECT("mygpimage",cFile) && create my subclass of gpimage, with some jpg
THROW "CreateFromImage "+cFilename
*Now modify the JPG by writing some text on it
oGraphics.DrawStringA("Hi There From Stream!"+TRANSFORM(DATETIME()),;
CREATEOBJECT("gpsolidbrush",0xffffffff)) && change the solidbrush color
CREATE CURSOR PicCursor (name c(10), pic blob) && A cursor into which we can store the blob
IF .f. && If we're using a temp file, we'd write then read the file
REPLACE pic WITH FILETOSTR(cOutFile)
ELSE && write to a stream: avoid the file I/O
nStm=ox.CreateStream(1) && Create stream nStm
IF !oImage.SaveToStream(nStm,"image/jpeg") && Call the new method to save new jpg to stream
?"Err saving stream"
ox.ReadStream(nStm,"pic",0) && Read stream into blob field directly
*Now create a form and display the modified blob, which was never written to disk
PUBLIC oForm as Form
oForm.img.height = oForm.Height
oForm.img.PictureVal=pic && use the blob field directly
DEFINE CLASS MyGpImage as gpimage OF HOME()+"ffc\_gdiplus"
*This method is just a copy of SaveToFile with a minor mod to save to stream
PROCEDURE SaveToStream(nStream as Integer,tvCLSIDEncoder, rvEncoderParams)
* Encoder may be varbinary, or string
lqCLSIDEncoder = m.tvCLSIDEncoder
lqCLSIDEncoder = This.StringToGUID( m.tvCLSIDEncoder)
lqCLSIDEncoder = This.GetEncoderCLSID(m.tvCLSIDEncoder)
declare integer GlobalFree in kernel32.dll integer nHandle
lnEncoderParamsPtr = This.getEncoderParamsFromArray( m.lqCLSIDEncoder, @rvEncoderParams )
lnEncoderParamsPtr = This.getEncoderParamsFromString( m.lqCLSIDEncoder, @rvEncoderParams )
case vartype(m.rvEncoderParams)=='L' and !m.rvEncoderParams
lnEncoderParamsPtr = 0
error 11 && function argument
DECLARE integer GdipSaveImageToStream IN gdiplus integer nImage,integer nStream,string qEncoder, integer nEncoderParamsPtr
This.gdipStatus = GdipSaveImageToStream ( ;
, nStream ;
, m.lqCLSIDEncoder ;
, m.lnEncoderParamsPtr )
return 0 == This.gdipStatus