• Welcome to SQLitening Support Forum.
 

News:

Welcome to the SQLitening support forums!

Main Menu

Embed Image Using SQLitening & DDOC

Started by Fredrick Ughimi, June 05, 2017, 12:57:31 PM

Previous topic - Next topic

Fredrick Ughimi

Great! Thanks CJ.

Been distracted with other part of the application.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

Working on allowing pathed files from client.

Fredrick Ughimi

>>Working on allowing pathed files from client.

Oh ok.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

#18
This version still needs to write images back and forth to the local disk to load into ddoc, yuck.
Because of this the logic had to be changed for client/server.
This is fast without the server, but leaves something to be desired running on a server.

The %TestMode variable shows the pushing and popping the database back and forth between local/server.
I'm not sure if that is killing the performance.  I'll work on this more when time permits.
Switching between local and remote is now very safe, but not sure it is implemented well.

Need to investigate ddoc source or perhaps these links:
https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/53636-some-png-images-display-oddly?t=52321&highlight=GdipDrawImageRect

https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/49073-gdi-question?t=47870

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/52507-gdi-image-save-load-from-to-buffer



#COMPILE EXE
#DIM ALL
$Server = "192.168.0.2":%Port=0  'remark to run without server

%AddClients=20
%TestMode = 0  'messagebox each time switch between local and server mode
%ClientServerFlag = 9999
#INCLUDE "ddoc_p32.inc" 'requires ddoc32.dll and ddoc_jpg.dll
#INCLUDE "\sql\bin\sqlitening.inc"
THREADED ClientServer,CurrentMode AS LONG 'need to know when switching local/remote at all times
'-------------------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG 'ddocsample3.bas
LOCAL x, AddClients AS LONG
#IF %DEF($Server)
slConnect $Server,%Port:ClientServer=%ClientServerFlag:CurrentMode=%ClientServerFlag
#ENDIF

slOpen "ddoc.db3","C"
IF %AddClients > 0 THEN
  slexe "drop table if exists ClientTable"
  slexe "drop table if exists ImageTable"
END IF

slexe "create table if not exists ClientTable(ClientKey integer primary key,ClientName)
slexe "create Table if not exists ImageTable(key UNIQUE, picture BLOB)"

IF %AddClients > 0 THEN
  AddClients = %AddClients
  slexe "begin immediate"   'add some clients and images
  FOR x = 1 TO AddClients
   slexe USING$("insert into ClientTable values(null_,#)",x)
   AddImage "\arm\heidi3.bmp","ImageTable",FORMAT$(x)
  NEXT
  slexe "end"
END IF

? "Ready to TestDDoc",%MB_SYSTEMMODAL,"Created tables"
TestDDoc
'Test
END FUNCTION

FUNCTION TestDDoc AS LONG
LOCAL sql AS STRING
LOCAL x,y,x2,y2 AS SINGLE
LOCAL PrintPreview,DefaultZoom,TheBin,EndCode,iHandle,counter,hpix AS LONG
LOCAL docTitle   AS ASCIIZ * 66
LOCAL outputfile AS ASCIIZ * 66
LOCAL skey AS STRING
LOCAL sKeyArray() AS STRING
LOCAL result AS LONG

DIM sClientNumberArray() AS STRING   'get all client numbers into an array

sql =  "select ClientKey from ClientTable"
slselAry sql,sClientNumberArray(),"Q9"
IF UBOUND(sClientNumberArray) < 1 THEN ? "No clients":EXIT FUNCTION

#IF %DEF($Server)
DocTitle = "CLIENT/SERVER MODE " + $Server + " on port"' + STR$(%Port)
#ELSE
   docTitle = "LOCAL MODE"
#ENDIF

PrintPreview = 1
DefaultZoom = %DDOC_ZOOM75
TheBin = %DDOC_Bin_Auto
IF PrintPreview THEN
  endCode = %DDOC_END_VIEW + %DDOC_END_DELETE
ELSE
  endCode = %DDOC_END_PRINT + %DDOC_END_DELETE
  DefaultZoom+= %DDOC_VIEWBUILD 'so no dialog will appear
END IF

ihandle = dpStartDoc(0,DocTitle,outputfile,%DDOC_INCH,%DDOC_PAPER_LETTER,%DDOC_SYSTEM_DEFAULT,TheBin,DefaultZoom + %DDOC_VIEWBUILD)

slSelAry sql,sKeyArray(),"Q9c"
IF slGetErrorNumber THEN ? slGetError,%MB_ICONERROR OR %MB_SYSTEMMODAL,"No images will print"

FOR counter = 1 TO UBOUND(sKeyarray)
  skey = skeyArray(counter)
  x =  0    'upper left column
  y  = 0    'upper left row
  x2 = 3    'low right column
  y2 = 5    'low right row
  result = slAddGraphic(iHandle,hpix,"ImageTable","picture",sKey,x,y,x2,y2)

  IF counter<> UBOUND(sKeyArray)THEN
   dpNewPage iHandle,  %ddoc_PAPER_A4, %DDOC_PORTRAIT, %DDOC_BIN_AUTO
  END IF
NEXT
dpEndDoc iHandle, EndCode
'SHELL ENVIRON$("COMSPEC") + " /C taskkill.exe /F /T /IM ddoc.exe",0
END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION AddImage(sImageFile AS STRING,sImageTable AS STRING,sKey AS STRING) THREADSAFE AS LONG

LOCAL result AS LONG
LOCAL sBlob AS STRING

result = slGetFileLocal(sImageFile,sBlob)
IF result THEN EXIT FUNCTION

FUNCTION = BlobToTable(sImageTable,sKey,sBlob)

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION BlobToTable(sImageTable AS STRING,sKey AS STRING,sBlob AS STRING) AS LONG

slExeBind "Replace into " + sImageTable + " values(?,?)", _
                                  slBuildBindDat(sKey,"T") + _
                                  slBuildBindDat(sBlob)
IF slGetChangeCount <> 1 THEN
   ? "Image key " + sKey,,"slPutImage Error"
   FUNCTION = 1
END IF

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION slGetImageFromTable(sImageTable AS STRING,sImageColumn AS STRING, sKey AS STRING) AS STRING
LOCAL sql AS STRING, counter AS LONG
slSetProcessMods "E2"
sql = "select " + sImageColumn + " from " + sImageTable + " where key ='" + sKey + "'"
slsel sql
IF slGetRow THEN FUNCTION = slf(1) ELSE ? "key not found" + $CR + sql,,FUNCNAME$:EXIT FUNCTION
END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION slGetFileLocal(sImageFile AS STRING,sBlob AS STRING) THREADSAFE AS LONG

LOCAL ModeBeforeHere AS LONG
ModeBeforeHere = CurrentMode
IF ModeBeforeHere <> 0 THEN SwitchLocal 'we must be local mode
FUNCTION = slGetFile(sImageFile,sBlob,"C E0")
IF ModeBeforeHere = %ClientServerFlag THEN SwitchRemote 'switch back to remote

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION slPutFileLocal(sTempFile AS STRING,sBlob AS STRING) THREADSAFE AS LONG

LOCAL ModeBeforeHere,result AS LONG
ModeBeforeHere = CurrentMode
IF ModeBeforeHere <> 0 THEN SwitchLocal 'we must be local mode
FUNCTION = slPutFile(sTempFile,sBlob,"CT E0")
IF slGetErrorNumber THEN ? slGetError,,FUNCNAME$

IF ModeBeforeHere = %ClientServerFlag THEN SwitchRemote 'switch back to remote

END FUNCTION
'-------------------------------------------------------------------------------------------------
FUNCTION slAddGraphic(iHandle AS LONG,hPix AS LONG,sImageTable AS STRING,sImageColumn AS STRING,sKey AS STRING, _
                      x AS SINGLE,y AS SINGLE,x2 AS SINGLE,y2 AS SINGLE) AS LONG

  LOCAL sBlob          AS STRING
  LOCAL result         AS LONG
  LOCAL sTempFile      AS STRING

  sBlob = slGetImageFromTable(sImageTable,sImageColumn,sKey)
  IF LEN(sBlob) = 0 THEN
    BEEP
    '? "zero length blob",,FUNCNAME$
    FUNCTION = 98
    EXIT FUNCTION
  END IF

  sTempFile = FORMAT$(TIMER) + ".tmp"
  result = slPutFileLocal(sTempFile,sBlob)  '6/11/17 wrap in threadsafe transaction

  FUNCTION = result
  IF result THEN
    ? "slPutError" + STR$(result),,FUNCNAME$
    EXIT FUNCTION
  END IF

  hpix = dpAddGraphic(ihandle,sTempFile+$NUL)
  KILL sTempFile
  IF hpix THEN
   dpDrawGraphic ihandle, hpix,x,y,x2,y2 'these values should be passed
  ELSE
   ? "Error getting graphic " + sTempFile,%MB_SYSTEMMODAL,FUNCNAME$
  END IF

END FUNCTION
'-------------------------------------------------------------------------------------------------
SUB SwitchLocal THREADSAFE

IF CurrentMode = 0 THEN EXIT SUB 'aleady in local mode
IF %TestMode THEN ? FUNCNAME$
slPushDatabase
slSetProcessMods "L0"
currentMode = 0

END SUB
'-------------------------------------------------------------------------------------------------
SUB SwitchRemote THREADSAFE

IF CurrentMode = %ClientServerFlag THEN EXIT SUB 'already in remote mode
IF %TestMode THEN ? FUNCNAME$
slPopDatabase
slSetProcessMods "L1"
CurrentMode = %ClientServerFlag

END SUB

Fredrick Ughimi

Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

Hello,

Been thinking.

I normally read images from the SQLitening Database and place them on the Picture Control. Like this:


sPicture = slFN("Picture")
VD_IPictureBox_LoadImageByMem(hfrmDPRRegistrationForm, %ID_FRMDPRREGISTRATIONFORM_VD_IPICTUREBOX1, StrPtr(sPicture), Len(sPicture)) 


I wish I could just use it to place the image on DDOC. Is there anyway to use it with dpAddGraphic?

Best regards,
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

VD_IPictureBox_LoadImageByMem() is wrapper function for PwrDev.
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

Fredrick Ughimi

#22
Hello CJ,

I am thinking of storing the images in a disk file.

The thing is, I am printing a Picture and a Name on the same page and can't figure out how to correctly match the appropriate Picture with the Name if the images are stored in disk file. I intend printing multiple pages at a go using a BatchNo.

Any ideas? Thank you for your responses.

Best regards,
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

How about an index to the locations in the flat file with any captions, titles, date you need by using a Table

slexe "create Table if not exists LocateImageTable(key UNIQUE, StartByteColumn,LengthColumn)"

Fredrick Ughimi

Hello CJ.

Quote
How about an index to the locations in the flat file with any captions, titles, date you need by using a Table
slexe "create Table if not exists LocateImageTable(key UNIQUE, StartByteColumn,LengthColumn)"

I guess this is in response to my last post before this.

Its a bit confusing. A flat file using a Table like in SQLite?
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet

cj

Quote
I am thinking of storing the images in a disk file.

The thing is, I am printing a Picture and a Name on the same page and can't figure out how to correctly match the appropriate Picture with the Name if the images are stored in disk file. I intend printing multiple pages at a go using a BatchNo.
Associate the picture and name with the starting byte in the file.
The table used to be an index to the file can contain anything and even the image.

Fredrick Ughimi

Hello CJ,

How do I read the disk file in DDOC?

My attemp...


#COMPILE EXE
#DIM ALL

#INCLUDE "sqlitening.inc"
#INCLUDE "DDOC_P32.INC"

GLOBAL sFileName AS STRING

%vbBlack = &H0&

FUNCTION PBMAIN () AS LONG
  LOCAL sPicture, sPixNamePath, sPixName, sCertificateNo, sName, sBatchNo AS STRING
  LOCAL Errorcode&

  sCertificateNo = "309"
  sName = "Mark Ogbe"
  sBatchNo = "122"
  sFileName = "PictureX"

  slOpen ("CharkinDB.db3","C")

  slExe BUILD$("Create Table If Not Exists tblDPRReg(CertificateNo TEXT, Name TEXT, BatchNo TEXT, PixName TEXT, Picture BLOB)")
  slExe "Create UNIQUE Index If Not Exists DPRRegndx ON tblDPRReg(CertificateNo)"

  sPixName = "Ughimi.jpg"

  sPixNamePath = "C:\Charkin ERP\Pictures\" & sPixName

   slGetFile sPixNamePath, sPicture, "E1" '9216 bytes

        Errorcode& = slExeBind(slBuildInsertOrUpdate("tblDPRReg", "?" & $NUL & "?" & $NUL & "?" & $NUL & "?" & $NUL & "?"), _
          slBuildBindDat(sCertificateNo, "T") & _
          slBuildBindDat(sName, "T") & _
          slBuildBindDat(sBatchNo, "T") & _
          slBuildBindDat(sPixName, "T") & _
          slBuildBindDat(sPicture, ("B")),"E")

          slPutFile sFileName, sPicture, "C"

          ReportCertificate()

END FUNCTION

FUNCTION ReportCertificate() AS LONG
      LOCAL LineSpacing!,iHandle%,Row!,TopMargin!,PageSize!,PageNumber&
      LOCAL detailline AS ASCIIZ * 200
      LOCAL SNo AS LONG
      LOCAL sBatchNo AS STRING
      LOCAL hpix AS LONG

      LineSpacing = .2
      TopMargin   = .5
      PAGESIZE    = 10

      ihandle% = dpStartDoc(0,"Mega-Net CharkinERP - Certificate Report","",%DDOC_INCH, %DDOC_PAPER_A4, %DDOC_PORTRAIT, %DDOC_SYSTEM_DEFAULT, %DDOC_BIN_AUTO OR %DDOC_ALLOWSMTP OR %DDOC_ALLOWSAVE OR %DDOC_ZOOMFIT)
      IF ihandle < 1 THEN
           MSGBOX "Could not StartDoc. Error number" + STR$(Ihandle)
           GOTO EndProgram
      END IF

      dpSetTabs iHandle, "L.5W1 L1.1W1 L2.5W4 L5.5W2"

      ROW = PAGESIZE                            'force new heading

      sBatchNo = "122"

     slSel "SELECT * FROM tblDPRReg WHERE BatchNo = '" + sBatchNo  + "'"

     DO WHILE slGetRow()

        IF ROW => PAGESIZE THEN
           IF PageNumber THEN      'If first page don't need a new page
              dpNewPage iHandle%,  %ddoc_PAPER_A4, %DDOC_PORTRAIT, %DDOC_BIN_AUTO
           END IF
           INCR PageNumber
           dpFont ihandle%, %DDOC_FONTNORMAL + %DDOC_FONTBOLD, 20, %vbBlack, "Arial"

           dpText IHandle%, 4.25, 4.6, %DDOC_CENTER, "" + slFN("Name")

           hpix = dpAddGraphic(ihandle%, EXE.PATH$ & "PictureX.dat") ' This is where I am having issues
           IF hpix THEN
              dpDrawGraphic ihandle%, hpix,6.4,6.1,7.8,11
           END IF
           'dpFont ihandle%, %DDOC_FONTNORMAL + %DDOC_FONTUNDERLINE + %DDOC_FONTBOLD, 10, %vbBlack, "Arial"
           'dpTabText iHandle%, Row + .7, "S/No" + $Tab + "CourseCode" + $Tab + "Course" + $Tab + "Amount"
           dpFont ihandle%, %DDOC_FONTNORMAL, 9, %vbBlack, "San Serif"
           ROW =  ROW + LineSpacing * 2
        END IF

        ROW = ROW + LineSpacing                    'increment line counter

      LOOP

      dpEndDoc iHandle%, %DDOC_END_VIEW + %DDOC_END_PRINT
    EndProgram:
END FUNCTION
Fredrick O. Ughimi<br /><br />fughimi@gmail.com<br />- Freedom lies in being bold -- Robert Frost, Poet