Imprimir imagen guardada en tabla SQL

Post Reply
jpcavagnaro
Posts: 35
Joined: Tue Oct 11, 2016 1:02 pm

Imprimir imagen guardada en tabla SQL

Post by jpcavagnaro »

Hola buen día, necesito imprimir una imagen guardada en una tabla sql, que la guarde así:

oRsFoto:Fields( "Foto" ):Value := STRTOHEX( MEMOREAD( aFiles[ i ] ) )

Estoy tratando de imprimirla así: ImprimirFoto( oRsFoto:Fields( "Foto" ):Value )

Function ImprimirFoto(wfoto)
PRINT oPrn PREVIEW MODAL
PAGE
oPrn:SayImage(0,0, wfoto, oPrn:nHorzRes(), oPrn:nVertRes() , nil, .t. )
ENDPAGE
ENDPRINT

retu .t.

No da error, pero muestra una hoja en blanco.

Saludos
Jorge
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

This should work, if you created the table with FOTO as BLOB datatype.
Regards

G. N. Rao.
Hyderabad, India
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Imprimir imagen guardada en tabla SQL

Post by Rick Lipkin »

Rao

I am using MS Sql Server .. there is no "Blob Type" .. however VarBinary(max) works for me ..

Rick Lipkin
jpcavagnaro
Posts: 35
Joined: Tue Oct 11, 2016 1:02 pm

Re: Imprimir imagen guardada en tabla SQL

Post by jpcavagnaro »

Hola la imagen está como VarBinary (max) y no funciona.

Lo solucione guardando la imagen en un temporal.jpg e imprimir desde ese temporal y funciona perfecto.

Saludos.
Jorge
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

Can you please let us know if you are using MySql or MSSQL and also the version of FWH you are using?
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

jpcavagnaro wrote:Hola la imagen está como VarBinary (max) y no funciona.

Lo solucione guardando la imagen en un temporal.jpg e imprimir desde ese temporal y funciona perfecto.

Saludos.
Jorge
Not necessary.
This is an example for MSSQL.
This sample uses MSSQL server in the cloud provided by FiveTech for demonstration purposes.
Please copy the sample to \fwh\samples folder and build with buildx.bat or buildh.bat

Code: Select all

#include "fivewin.ch"

function Main()

   local oCn, nVer, cSql, oRs, oPrn

   oCn   := FW_OpenAdoConnection( { "MSSQL", "208.91.198.196", "gnraore3_", "fwhmsdemo", "fwh@2000#" }, .t. )
   if oCn == nil
      ? "Connect fail"
      return nil
   end
   nVer     := Val( oCn:Properties( "DBMS Version" ):Value )

   // CREATE TABLE
   TRY
      oCn:Execute( "DROP TABLE [adoimage]" )
   CATCH
   END

if nVer < 9
//
TEXT INTO cSql
CREATE TABLE [adoimage] (
   [ID] INT IDENTITY( 1, 1 ) PRIMARY KEY,
   [NAME] VARCHAR ( 10 ),
   [FOTO] IMAGE
)
ENDTEXT
//
else
//
TEXT INTO cSql
CREATE TABLE [adoimage] (
   [ID] INT IDENTITY( 1, 1 ) PRIMARY KEY,
   [NAME] VARCHAR ( 10 ),
   [FOTO] VARBINARY(max)
)
ENDTEXT
//
endif

   oCn:Execute( cSql )

   // ADD ONE RECORD TO THE TABLE
   oRs   := FW_OpenRecordSet( oCn, "adoimage" )
   oRs:AddNew()
   oRs:Fields( "NAME" ):Value := "OLGA1"
   oRs:Fields( "FOTO" ):Value := STRTOHEX( MEMOREAD( "c:\fwh\bitmaps\olga1.jpg" ) )
   oRs:Update()
   oRs:Close()

   // OPEN THE TABLE AND PRINT
   oRs   := FW_OpenRecordSet( oCn, "adoimage" )

   PRINT oPrn PREVIEW
   PAGE
   oPrn:SayImage(0,0, oRs:Fields( "FOTO" ):Value, 1800, 2400, nil, .t. )
   ENDPAGE
   ENDPRINT

   oRs:Close()
   oCn:Close()

return nil
 
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

Hola la imagen está como VarBinary (max) y no funciona.
This depends on the MSSQL server version.
For versions less than 9.0, we need to use

Code: Select all

   [FOTO] IMAGE
 
and for later versions, we can use

Code: Select all

   [FOTO] VARBINARY(max)
 
Please see the sample above.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

We have seen that the SQL statement required to create a table can be different for different versions of MSSQL. Naturally it is different for different RDBMS like MSSQL, MYSQL, ORACLE, etc.

For example the SQL for the same table can be:

Code: Select all

CREATE TABLE `adoimage` (  
   `ID` INT AUTO_INCREMENT PRIMARY KEY, 
   `NAME` VARCHAR ( 10 ), 
   `FOTO` LONGBLOB
)
 
For MSACCESS:

Code: Select all

CREATE TABLE [adoimage] (  
  [ID] AUTOINCREMENT PRIMARY KEY, 
  [NAME] VARCHAR ( 10 ), 
  [FOTO] LONGBINARY 
)
 
Instead of writing SQL statements for creation of tables on our own, we recommend using FWH function

Code: Select all

FWAdoCreateTable( cTable, aStruct, oCn, [lAddAutoInc := .t.] )
 
aStruct is a normal DBF style structure array we are all very familiar with. Data type "M" creates a long text field and datatype "m" creates a long binary field.
This function internally creates an sql statement appropriate for the RDBMS and its version and we are relieved from the trouble of finding the correct datatype.

The next sample demonstrates using this function for MSSQL, MYSQL and MSACCESS. Using FWH provided ADO functions, we can write code that works with different RDBMS (also versions) without changing our application code:

Code: Select all

#include "fivewin.ch"

function Main()

   local nCh, oCn, oRs, oPrn
   local cTable   := "adoimage"
   local aImages  := { "c:\fwh\bitmaps\olga1.jpg", "c:\fwh\bitmaps\sea.bmp" }

   nCh   := Alert( "RDMBS", { "MySQL", "MSSQL", "MSACCESS" }, "Select" )

   if nCh == 1
      oCn   := FW_OpenAdoConnection( { "MYSQL", "54.37.60.32", "fwh", "fwhuser", "fwh202006" }, .t. )
   elseif nCh == 2
      oCn   := FW_OpenAdoConnection( { "MSSQL", "208.91.198.196", "gnraore3_", "fwhmsdemo", "fwh@2000#" }, .t. )
   elseif nCh == 3
      oCn   := FW_OpenAdoConnection( "xbrtest.mdb" )
   else
      return nil
   endif

   if oCn == nil
      ? "Connection error"
      return nil
   endif

   TRY
      oCn:Execute( "DROP TABLE " + cTable )
   CATCH
   END

   FWAdoCreateTable( cTable, { { "NAME", "C", 10, 0 }, { "FOTO", "m", 8, 0 } }, oCn )

   oRs   := FW_OpenRecordSet( oCn, cTable )

   // Append one image
   oRs:AddNew()
   oRs:Fields( "NAME" ):Value := Upper( cFileNoExt( aImages[ 1 ] ) )
   oRs:Fields( "FOTO" ):Value := STRTOHEX( MEMOREAD( aImages[ 1 ] ) ) // Harbour: HB_STRTOHEX(...)
   oRs:Update()

   // Append second image. Simpler method
   oRs:AddNew( { "NAME", "FOTO" }, { Upper( cFileNoExt( aImages[ 2 ] ) ), HB_STRTOHEX( MEMOREAD( aImages[ 2 ] ) ) } )

   oRs:Close()

   oRs   := FW_OpenRecordSet( oCn, cTable )
   XBROWSER oRs
   oRs:MoveFirst()

   PRINT oPrn PREVIEW

   PAGE
   @ 1,1 PRINT TO oPrn IMAGE oRs:Fields( "FOTO" ):Value SIZE 6,4 INCHES
   oRs:MoveNext()
   @ 6,1 PRINT TO oPrn IMAGE oRs:Fields( "FOTO" ):Value SIZE 6,4 INCHES
   ENDPAGE

   ENDPRINT

   oRs:Close()

return nil
For all RDBMSs it is the same code and same result:
Image
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
richard-service
Posts: 583
Joined: Tue Oct 16, 2007 8:57 am
Location: New Taipei City, Taiwan
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by richard-service »

nageswaragunupudi wrote:
Hola la imagen está como VarBinary (max) y no funciona.
This depends on the MSSQL server version.
For versions less than 9.0, we need to use

Code: Select all

   [FOTO] IMAGE
 
and for later versions, we can use

Code: Select all

   [FOTO] VARBINARY(max)
 
Please see the sample above.
Mr.Rao
MySQL v8.0.13 same as support it.
Regards,

Richard

Harbour 3.2.0dev (r1904111533)/xHarbour 1.2.3 Intl. (SimpLex) (Build 20180818) => Borland C++ v7.4
xHarbour 0.99.71 (SimpLex) => Borland C++ v5.5
MySQL v5.7 /ADS v12
Harbour 3.2.0dev (r1603181642) => Borland C++ v7.4 64bit
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Imprimir imagen guardada en tabla SQL

Post by Rick Lipkin »

Rao

I have looked for LONGBINARY in the field selections of Ms Access.mdb and .ACCDB and that data type is not an option .....

Image

I am using ole object a field to store pictures in Ms Access ....

Image

???

Thanks
Rick Lipkin
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

richard-service wrote:
nageswaragunupudi wrote:
Hola la imagen está como VarBinary (max) y no funciona.
This depends on the MSSQL server version.
For versions less than 9.0, we need to use

Code: Select all

   [FOTO] IMAGE
 
and for later versions, we can use

Code: Select all

   [FOTO] VARBINARY(max)
 
Please see the sample above.
Mr.Rao
MySQL v8.0.13 same as support it.
The discussion about field types IMAGE and VARBINARY(max) are for MSSQL ( Micorosoft SQL Server ) but not for MySQL.
Because you are using MySQL, this does not apply to you.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Imprimir imagen guardada en tabla SQL

Post by nageswaragunupudi »

Rick Lipkin wrote:Rao

I have looked for LONGBINARY in the field selections of Ms Access.mdb and .ACCDB and that data type is not an option .....

Image

I am using ole object a field to store pictures in Ms Access ....

Image

???

Thanks
Rick Lipkin
https://docs.microsoft.com/en-us/sql/od ... rver-ver15

You can choose OLE and then store any binary data in the field. But when we create the table using SQL, better we use the datatype VARBINARY or LONGBINARY.

Also see
https://codekabinett.com/rdumps.php?Lan ... s-database
Regards

G. N. Rao.
Hyderabad, India
Post Reply