Kbase P20196: How to show images stored in MS SQL database?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  27/02/2003 |
|
Status: Unverified
GOAL:
How to show images stored in MS SQL database?
FACT(s) (Environment):
Progress 9.1x
FACT(s) (Environment):
MS SQL Dataserver
FIX:
The following example will work for Northwind SQL database. Images are stored in "categories" table, field "picture".
78 bytes are stored at the begining of the "picture" field which are not part of the image itself. We need to ignore these 78 bytes.
Image information should be restored on the disk so it can be displayed with IMAGE widget in 4GL.
FUNCTION hexValue RETURNS INTEGER( pc AS CHARACTER ):
CASE pc:
WHEN "A" THEN RETURN 10.
WHEN "B" THEN RETURN 11.
WHEN "C" THEN RETURN 12.
WHEN "D" THEN RETURN 13.
WHEN "E" THEN RETURN 14.
WHEN "F" THEN RETURN 15.
OTHERWISE
RETURN INTEGER( pc ).
END CASE.
END FUNCTION.
FUNCTION chex2byte RETURNS INTEGER ( vc as character ):
RETURN 16 * hexValue( SUBSTRING( vc, 1, 1 ) )
+ hexValue( SUBSTRING( vc, 2, 1 ) )
.
END FUNCTION.
/* this procedure will create the image file and load the image into */
/* the IMAGE widget */
PROCEDURE showPicture:
DEF VAR pImg AS MEMPTR.
DEF VAR cImg AS CHARACTER.
DEF VAR cByte AS CHARACTER.
DEF VAR iByte AS INTEGER.
DEF VAR lImg AS INTEGER.
DEF VAR vi AS INTEGER.
cImg = SUBSTRING( categories.picture, 3 ). /* ignore '0x' */
lImg = INTEGER( LENGTH( cImg ) / 2 ).
/* sOut is a STREAM defined in the declaration part */
OUTPUT STREAM sOut TO my.img.
DO WHILE LENGTH( cImg ) > 0 :
cByte = SUBSTRING( cImg, 1, 2 ).
iByte = chex2byte( cByte ).
vi = vi + 1.
IF vi > 78 THEN DO: /* ignore first 78 bytes */
IF iByte <> 0 THEN
PUT STREAM sOut CONTROL CHR(iByte) .
ELSE
PUT STREAM sOut CONTROL NULL(1) .
END.
cImg = SUBSTRING( cImg, 3 ).
END.
OUTPUT STREAM sOut CLOSE.
showImg:LOAD-IMAGE( "my.img" ) IN FRAME {&FRAME-NAME}.
END PROCEDURE.