Guardar Imagenes en una tabla

Guardar Imagenes en una tabla

Postby leandro » Tue Jan 15, 2008 12:27 am

Amigos del foro tengan buena noche. Como dice el encabezado, me gustaria saber si puedo almacenar una imagen en una dbf y si se puede que tipos de imagenes puedo almacenar.

Al mismo tiempo quiero saber que campo debo crear en una tabla de sql para almacenar imagenes.

De antemano gracias. :D
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1489
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Postby Willi Quintana » Tue Jan 15, 2008 8:13 pm

Para MySQL, imagino que tambien va para SQL-MS, es algo lento pero me funciona a 100......

Code: Select all  Expand view
...
cfile := "imagen.jpg"                               // tomamos la imagen
cImagen := MEMOREAD(cFile)                 // la lleemos con memoread
cChr    := Text2Hex(cImagen)                // la convertimos a hexadecimal
cFile   := STRTRAN(cFile,"\","/")              // depuramos los slash y zas

cUpdate := "
UPDATE fichas SET fotografia ='" + cFile + "' WHERE .... "
.........


para leerla desde la base de datos...
...
cChr := ofichas:FieldGet(4)       // obtenemos la cadena almacenada
cImagen := Hex2Text(cChr)     // convertimos a texto
EscribeTxt(cFile, cImagen)       // la guardamos en un archivo
oFoBmp:LoadBmp(cFile)           // i la levantamos con LoadBmp
oFoBmp:Refresh()
FERASE(cFile)
Return(oFoBmp)

Function EscribeTxt(cTxt, cFile, lModo, cRuta)
local hHandle, cTxTmp, lOk
DEFAULT cFile := "
nsql.txt"
DEFAULT lModo := .f.
DEFAULT cRuta := "
"

lOk := .f.
If lModo
  cTxTmp := "
"
  If File(cFile)
    hHandle := FOpen(cFile)
    FRead(hHandle, @cTxTmp, 64000)
    FCLose(hHandle)
    cTxt := ALLTRIM(cTxTmp) + CHR(13) + CHR(10) + cTxt
  EndIf
EndIf

hHandle := FCREATE(cFile)
FWrite(hHandle,cTxt)
FClose(hHandle)

Return(lOk)



Function Text2Hex(cFile)
local nLen, nCont, cChr, cBin
cBin := "
"
nLen := LEN(cFile)
FOR nCont := 1 TO nLen
   cChr := SUBSTR(cFile, nCont, 1)
   cBin := cBin + STRTOHEX(cChr)
NEXT nCont

Return(cBin)
//-------------------------------------------------------------------------------------------
Function Hex2Text(cFile)
local nLen, nCont, cChr, cBin
cBin := "
"
nLen := LEN(cFile)
FOR nCont := 1 TO nLen STEP 2
   cChr := SUBSTR(cFile, nCont, 2)
   cBin := cBin + HEXTOSTR(cChr)
NEXT nCont

Return(cBin)



//====================================================================================================
#pragma BEGINDUMP
#include "
hbapi.h"
#include "
hbapiitm.h"    //#include "hbfast.h"
#include "
hbstack.h"
#include "
hbdefs.h"
#include "
hbvm.h"
#include "
hbapierr.h"

HB_ULONG HB_EXPORT hb_hextonum(char *cHex)
{
   HB_ULONG   ulNum = 0;
   char       c;
   int        iDigit;

   while ( *cHex && (*cHex == ' ') ) cHex++;

   while ( *cHex )
   {
      ulNum <<= 4;

      c = *cHex;
      if ( c >= '0' && c <= '9' )
      {
         iDigit = c - '0';
      }
      else if ( c >= 'A' && c <= 'F' )
      {
         iDigit = c - 'A' + 10;
      }
      else if ( c >= 'a' && c <= 'f' )
      {
         iDigit = c - 'a' + 10;
      }
      else
      {
         ulNum = 0;
         break;
      }
      ulNum += iDigit;
      cHex++;
   }

   return ulNum;
}

HB_FUNC( NUMTOHEX )
{
   int       iDigit;
   char      ret[ 33 ];
   int       iLen, iDefaultLen;
   HB_ULONG  ulNum;

   if( ISNUM( 2 ) )
   {
      iLen = hb_parni( 2 );
      iLen = ( iLen < 1 ) ? 1 : ( ( iLen > 32 ) ? 32 : iLen );
      iDefaultLen = 0;
   }
   else
   {
      iLen = 32;
      iDefaultLen = 1;
   }

   if( ISNUM( 1 ) )
   {
      ulNum = hb_parnint( 1 );
   }
   else if ( ISPOINTER( 1 ) )
   {
      ulNum = (HB_PTRDIFF) hb_parptr( 1 );
   }
   else
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "
NUMTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
      return;
   }

   ret[ iLen ] = '\0';
   do {
      iDigit = (int) ( ulNum & 0x0F );
      ret[ --iLen ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );
      ulNum >>= 4;
   } while ( iDefaultLen ? ulNum > 0 : iLen > 0 );

   hb_retc( &ret[ iLen ] );
}


HB_FUNC( HEXTONUM )
{
   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "
HEXTONUM", 1, hb_paramError( 1 ) );
      return;
   }

   hb_retnint( hb_hextonum( hb_parc( 1 ) ) );
}


HB_FUNC( STRTOHEX )
{
   char   *cOutBuf;
   char   *cStr;
   char   *c;
   char   *cSep = "
";
   unsigned char  ucChar;
   ULONG  ul, ulLen, ulLenSep = 0;
   int    iDigit;

   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "
STRTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
      return;
   }

   if ( ISCHAR( 2 ) )
   {
     cSep = hb_parc( 2 );
     ulLenSep = hb_parclen( 2 );
   }

   cStr = hb_parc( 1 );
   ulLen = hb_parclen( 1 );
   c = cOutBuf = (char*) hb_xgrab( ulLen * 2 + ( ulLen - 1 ) * ulLenSep + 1 );

   for( ul = 0; ul < ulLen; ul++ )
   {
      if ( ulLenSep && ul )
      {
         memcpy( c, cSep, ulLenSep );
         c += ulLenSep;
      }

      ucChar = (unsigned char) cStr[ ul ];

      iDigit = (int) ( ucChar & 0x0F );
      c[ 1 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

      ucChar >>= 4;

      iDigit = (int) ucChar;
      c[ 0 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

      c += 2;
   }
   hb_retclen( cOutBuf, c - cOutBuf );
   hb_xfree( cOutBuf );
}


HB_FUNC( HEXTOSTR )
{
   char *cOutBuf, *cStr;
   char c;
   int  iByte, iFirst;
   ULONG  ul, ulLen, ulPos, ulAlloc;

   if( ! ISCHAR(1) )
   {
      hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "
HEXTOSTR", 1, hb_paramError( 1 ) );
      return;
   }

   cStr = (char *) hb_parc( 1 );
   ulLen = hb_parclen( 1 );
   ulAlloc = (int) ( ulLen / 2 );
   cOutBuf = (char *) hb_xgrab( ulAlloc + 1 );

   ulPos = 0;
   iByte = 0;
   iFirst = 1;

   for ( ul = 0; ul < ulLen; ul++ )
   {
      iByte <<= 4;

      c = *cStr++;
      if ( c >= '0' && c <= '9' )
      {
         iByte += c - '0';
      }
      else if ( c >= 'A' && c <= 'F' )
      {
         iByte += c - 'A' + 10;
      }
      else if ( c >= 'a' && c <= 'f' )
      {
         iByte += c - 'a' + 10;
      }
      else
      {
        continue;
      }

      iFirst ^= 1;
      if ( iFirst )
      {
        cOutBuf[ ulPos++ ] = (char) iByte;
        iByte = 0;
      }
   }
   hb_retclen( cOutBuf, ulPos );
   hb_xfree( cOutBuf );
}

#pragma ENDDUMP
User avatar
Willi Quintana
 
Posts: 1004
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 152 guests