How to print pictures and how to display pictures that are stored in a Blob field in Visual FoxPro 9.0 (895602)



The information in this article applies to:

  • Microsoft Visual FoxPro 9.0 Professional Edition

SUMMARY

Microsoft Visual FoxPro 9.0 introduces the Blob data type. You can use Blob fields to store binary data such as pictures. Pictures that are stored in a Blob field can be printed on reports and displayed on forms. This article describes how to print pictures and how to display pictures that are stored in a Blob field.

INTRODUCTION

Visual FoxPro 9.0 introduces the Blob data type. You can use Blob fields to store binary data of any kind and of indeterminate length. For example, you can store pictures in a Blob field.

You can print pictures that are stored in a Blob field on a report by using the Picture/OLE Bound control. To print pictures that are stored in a Blob field, you must set the control source of the Picture/OLE Bound control that is being used to an object reference. The object reference must be to an instance of an Image control. The PictureVal property of this instance must be set to a Blob field that contains the pictures.

You can display pictures that are stored in a Blob field on a form by using the Image control. You put the Image control directly onto the form, and you set the PictureVal property of the Image control. This addresses binding the Image control to the Blob field. However, additional steps are required to force the Image control to redraw as the record pointer moves in the table that contains the Blob field.

MORE INFORMATION

The following code samples are designed for Visual FoxPro 9.0. These code samples show how to print pictures and how to display pictures that are stored as binary data in a Blob field. To test these samples, follow these steps:
  1. Start Visual FoxPro 9.0, and create two new programs.
  2. Copy and then paste sample 1 into one of the new programs.
  3. Copy and then paste sample 2 into the other new program.
  4. Save and then run the programs.

Sample 1: Print pictures that are stored in a Blob field on a report

The following code sample shows how to print pictures that are stored as binary data in a Blob field on a report.
*------------ START CODE
*
*-----------------------------------
* AUTHOR:    Trevor Hancock
* CREATED:   03/04/05 01:03:07 P.M.
* ABSTRACT: Code from Microsoft Knowledge Base
*                 article 895602. Visual FoxPro code that shows how
*                 to use pictures that are
*                 stored in a BLOB field in a report.
*                 This is accomplished by using an
*                 object reference to an instance of
*                 an IMAGE class as the control source for
*                 an OLE Bound control on the report. The IMAGE
*                 object has its PictureVal property set to a BLOB
*                 field in a cursor.
*-----------------------------------
LOCAL lcDataDIR AS STRING, ;
	lcThisDir AS STRING, ;
	loRL AS REPORTLISTENER

lcDataDIR = HOME( ) + 'Samples\Tastrade\'
lcThisDir = ADDBS( JUSTPATH( SYS( 16 ) ) )

CD ( lcThisDir )
CLOSE DATA ALL
*-- Create a temp cursor with a few fields, one of which is a
*-- BLOB. Store pictures in this field as raw binary data.
SELECT CAST( ALLTRIM( First_Name ) AS VARCHAR ( 10 ) ) AS 'FNAME', ;
	CAST( ALLTRIM( Last_Name ) AS VARCHAR (10 ) ) AS 'LNAME', ;
	CAST( FILETOSTR( lcDataDIR + Photo_File ) AS BLOB ) AS 'PIC' ;
	FROM ( lcDataDIR + 'data\Employee.dbf' ) ;
	INTO CURSOR ReportTemp

*-- Close the table that you selected from. It is not needed.
USE IN SELECT( 'EMPLOYEE' )

*-- This calls a function that makes a report programmatically.
*-- This is included here just to make sure that this sample can be run
*-- as-is, without asking the developer to manually create a report.
MakeReport()

*-- Create an instance of the PreviewListener
*-- class defined later in this code.
*-- Call its custom InitBLOBImage() method,
*-- which creates an instance of an IMAGE object.
*-- This IMAGE has its PictureVal property set to the BLOB
*-- field ( 'ReportTemp.PIC' ) and its reference ( loRL.oBlobImage )
*-- is used as the control source for the OLE Bound control 
*-- on the report.
loRL = NEWOBJECT( 'PreviewListener' )
loRL.InitBLOBImage( 'ReportTemp.PIC' )

*-- Make sure that the cursor is selected,
*-- and then run the report to preview using
*-- the instance of our Report Listener.
SELECT ReportTemp
REPORT FORM BlobReport OBJECT loRL
CLOSE DATA ALL
RETURN





*--------------------------------
*-- There has to be some way of redrawing the
*-- picture in the IMAGE class as the record pointer
*-- in the report's driving cursor changes; it does not occur
*-- automatically. This could be done by a UDF() in the PrintWhen
*-- of the OLE Bound control on the report, or as is illustrated here,
*-- by a Report Listener BEFOREBAND() Event.
DEFINE CLASS PreviewListener AS REPORTLISTENER
	oBlobImage = NULL
	PicBlobFld = ''
	LISTENERTYPE = 1  && Preview Listener

	PROCEDURE InitBLOBImage(lpcBlobField AS STRING)
		THIS.PicBlobFld = lpcBlobField
		THIS.oBlobImage = NEWOBJECT( 'IMAGE' )
		THIS.oBlobImage.PICTUREVAL = THIS.PicBlobFld
	ENDPROC

	PROCEDURE BEFOREBAND( nBandObjCode, nFRXRecNo )
		*-- Before the DETAIL band is rendered, ;
		*-- just redraw the IMAGE object so that it has
		*-- the correct picture from the BLOB field.
		IF nBandObjCode = 4 && Detail band
			THIS.oBlobImage.PICTUREVAL =;
				EVALUATE( THIS.PicBlobFld )
		ENDIF
	ENDPROC
ENDDEFINE




*--------------------------------
*-- This function programmatically creates a report
*-- with an OLE Bound control and other fields. This is included
*-- only for demonstration purposes so this article code can stand-alone.
*-- Typically, you would create your own report manually by using
*-- the report designer.
FUNCTION MakeReport
	CREATE REPORT BlobReport FROM ReportTemp

	*-- Open the report file (FRX) as a table.
	USE BlobReport.FRX IN 0 ALIAS TheReport EXCLUSIVE
	SELECT TheReport

	*-- Increase the height of the Detail band
	*-- (ObjType = 9 & ObjCode = 4) to fit the
	*-- Picture/OLE Bound control that is inserted later.
	UPDATE TheReport SET Vpos = 0, Hpos = 0, HEIGHT = 23542;
		WHERE ObjType = 9 AND ObjCode = 4

	*-- Since you increased the height of the Detail Band, you need to move
	*-- the items from the footer down so they are back in the footer again.
	UPDATE TheReport SET Vpos = 29479.167 ;
		WHERE ( ObjType = 8 OR ObjType = 5 ) AND ;
		INLIST( EXPR, 'DATE()', '"Page "', '_PAGENO' )

	*-- Add a Picture/OLE Bound control to the report by inserting a
	*-- record with appropriate values. Using an object that is based on the EMPTY
	*-- class here and the GATHER NAME class later to insert the record makes it easier to
	*-- see which values line up to which fields (when compared to a large
	*-- SQL-INSERT command).
	LOCAL loNewRecObj AS EMPTY
	loNewRecObj = NEWOBJECT( 'EMPTY' )
	ADDPROPERTY( loNewRecObj, 'PLATFORM', 'WINDOWS' )
	ADDPROPERTY( loNewRecObj, 'Uniqueid', SYS(2015) )
	ADDPROPERTY( loNewRecObj, 'ObjType', 17 ) && "Picture/OLE Bound Control"
	ADDPROPERTY( loNewRecObj, 'NAME', 'loRL.oBlobImage' ) && The object ref to the IMAGE object.
	ADDPROPERTY( loNewRecObj, 'Hpos', 27500.000) && Place it in DETAIL band.
	ADDPROPERTY( loNewRecObj, 'Vpos', 3854.167)
	ADDPROPERTY( loNewRecObj, 'HEIGHT', 21354.167)
	ADDPROPERTY( loNewRecObj, 'WIDTH', 25104.167)
	ADDPROPERTY( loNewRecObj, 'DOUBLE', .T. ) && Picture is centered in the "Picture/OLE Bound Control"
	ADDPROPERTY( loNewRecObj, 'Supalways', .T. )
	*-- For the Picture/OLE Bound control, the contents of the OFFSET field specify whether
	*-- Filename (0), General field name (1), or Expression (2) is the source.
	ADDPROPERTY( loNewRecObj, 'Offset', 2 )

	*-- Add the Picture/OLE Bound control record to the report.
	APPEND BLANK IN TheReport
	GATHER NAME loNewRecObj MEMO

	*-- Clean up and then close the report table.
	PACK MEMO
	USE IN SELECT( 'TheReport' )
ENDFUNC
*
*------------ END CODE

Sample 2: Display pictures that are stored in a Blob field on a form

The following code sample shows how to display pictures that are stored as binary data in a Blob field on a form.
*------------ START CODE
*
*-----------------------------------
* AUTHOR:    Trevor Hancock
* CREATED:   03/04/05 01:03:07 P.M.
* ABSTRACT: Code from Microsoft Knowledge Base
*                 article 895602. Visual FoxPro code that shows how
*                 to display pictures that are
*                 stored in a BLOB field on a form.
*-----------------------------------
CLOSE DATA ALL

*-- Create a temp cursor with a BLOB field.
*-- Store pictures in this field as raw binary data.
SELECT 	;
	CAST( ;
	FILETOSTR( HOME( ) + 'Samples\Tastrade\' + Photo_File ) ;
	AS BLOB ) AS 'PIC' FROM ;
	HOME( ) + 'Samples\Tastrade\data\Employee.dbf' ;
	INTO CURSOR BlobTemp

*-- Close the table that you selected from. It is not needed.
USE IN SELECT( 'EMPLOYEE' )

*-- Create a form to display the pictures.
PUBLIC goForm
goForm = NEWOBJECT( 'Form' )

WITH goForm AS FORM
	*-- Add an IMAGE class to display the pictures.
	*-- This is done by setting the PICTUREVAL
	*-- property of the IMAGE.
	.ADDOBJECT( 'IMG1', 'Image' )
	.Img1.PICTUREVAL = BlobTemp.PIC

	*-- Add an EditBox to display the raw
	*-- binary data directly out of the BLOB field.
	*-- This is not required, just fun to see the raw data.
	.ADDOBJECT( 'Edt1', 'EditBox' )
	.Edt1.MOVE(.Img1.WIDTH + 5, 0, .Img1.WIDTH, .Img1.HEIGHT)
	.Edt1.CONTROLSOURCE = 'BlobTemp.PIC'

	*-- Add record navigation buttons so that you can
	*-- move records in the cursor. The NAV class
	*-- is defined later in this code.
	.ADDOBJECT( 'NAV', 'NavBtns' )
	.NAV.MOVE( (.WIDTH - .NAV.WIDTH) + 13 , .Edt1.HEIGHT + 2 )

	.WIDTH = .Img1.WIDTH + .Edt1.WIDTH + 7
	*-- Make the form pretty by setting a few anchors
	*-- and setting MinWidth/Height.
	.NAV.ANCHOR = 192
	.Edt1.ANCHOR = 15
	.MINWIDTH = .WIDTH
	.MINHEIGHT = .HEIGHT
	.AUTOCENTER = .T.
	.CAPTION = 'Picture and Raw BLOB Data'
	.SETALL( 'VISIBLE', .T. )
ENDWITH

goForm.SHOW(1)
CLOSE DATA ALL


*--------------------------------
*-- This is just a subclass of the FoxPro Foundation Class
*-- named _DataNavBtns (located in HOME() + '\FFC\_datanav.vcx').
*-- This is where you redraw the BLOB data displayed by the IMAGE
*-- class by resetting the PICTUREVAL property of that class
*-- just before skipping records. Unlike an OLE Bound control populated
*-- by a GENERAL field, the IMAGE does not redraw its BLOB data automatically
*-- as the record pointer moves. This is how you force it.
DEFINE CLASS NavBtns AS _datanavbtns OF HOME() + '\FFC\_datanav.vcx'
	BORDERWIDTH = 0
	PROCEDURE EnableDisableButtons
		THIS.PARENT.Img1.PICTUREVAL = BlobTemp.PIC
		DODEFAULT()
ENDDEFINE
*
*------------ END CODE

REFERENCES

For more information about the PictureVal property, visit the following Microsoft Web site:For more information, see the "Dockable Image Viewer" sample in the Visual FoxPro Solution samples application that is included with Visual FoxPro 9.0.

Modification Type:MinorLast Reviewed:3/25/2005
Keywords:kbhowto kbCodeSnippet KB895602 kbAudDeveloper