resource on hash no run with webview2 - RESOLVED

Post Reply
User avatar
Silvio.Falconi
Posts: 7235
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

resource on hash no run with webview2 - RESOLVED

Post by Silvio.Falconi »

Image

I load images and save on a Hash
then I use on html {IMGHOME} then replace the images before to release the chtml
If the images are on resources not load the images

I tried also with

hImages["IMGHOME"] := "data:image/png;base64," + hb_base64encode( GetResources("DASH_HOME") )

Please try this test


test.prg

Code: Select all | Expand

#include "fivewin.ch"

function Main()
   local oWnd, oWebView


 DEFINE WINDOW oWnd TITLE "Invoicing"

 BuildMainBar(oWnd)

 oWebView := TWebView2():New( oWnd )
 oWebView:SetHtml( Html() )


  SET MESSAGE OF oWnd TO "Invoicing app"  CENTER NOINSET

   ACTIVATE WINDOW oWnd  ;
      ON RESIZE ( oWebView:SetSize( WndWidth( oWebView:oWnd:hWnd ) - 17,;
                                    WndHeight( oWebView:oWnd:hWnd ) - 80 ) )
   RETURN NIL


Function Html()
     LOCAL hImages := LoadImages()  // Carica l'hash delle immagini
   LOCAL cHtml
   LOCAL cKey


  TEXT INTO cHtml
   <!DOCTYPE html>
   <html>
   <head>
     <title>Identify</title>
     <style>
     body {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      background-color: #F2F2F2;
      font-family: Arial, sans-serif;
      margin: 0;
     padding: 0;
    }

      .container {
         max-width: 400px;
         margin: 0 auto;
         padding: 40px;
         background-color: #FFFFFF;
         border-radius: 5px;
         display: flex;
         flex-direction: column;
         align-items: center;
         box-shadow: 0px 0px 44px rgba(0, 0, 0, 0.7) !important;
         overflow:none !important;
       }

       .logo {
         margin-bottom: 40px;
         display: flex;
         align-items: center;
       }

       .logo img {
         max-width: 100%;
         height: auto;
       }

       .btn {
         display: inline-block;
         padding: 12px 24px;
         background-color: #4CAF50;
         color: #FFFFFF;
         font-size: 16px;
         text-decoration: none;
         border-radius: 5px;
         transition: background-color 0.3s ease;
       }

       .btn:hover {
         background-color: #45A049;
       }

       .form-group {
         margin-bottom: 20px;
       }

       .form-group label {
         display: block;
         font-size: 16px;
         font-weight: bold;
         margin-bottom: 5px;
         color: #333333;
       }

       .form-group input {
         width: 100%;
         padding: 12px;
         font-size: 16px;
         border-radius: 5px;
         border: 1px solid #CCCCCC;
       }

       .btn {
         display: inline-block;
         padding: 12px 24px;
         background-color: #4CAF50;
         color: #FFFFFF;
         font-size: 16px;
         text-decoration: none;
         border-radius: 5px;
         transition: background-color 0.3s ease;
       }

       .btn:hover {
         background-color: #45A049;
       }

       body {
         background-color: #3498db;
       }

       .btn {
         background-color: #2980b9;
       }

       .btn:hover {
         background-color: #1a5276;

      .logo {
         margin-bottom: 40px;
         }

         .logo img {
         max-width: 100%;
         height: auto;
         }
       }

     .myinput {
       width: auto !important;
     }

     .mybtn {
       text-align:center;
     }

     </style>
   </head>
   <body>
     <div class="container">
       <div class="logo">
         <img src="{IMGHOME}" alt="Home">
       </div>

     </div>


   </body>
   </html>
   ENDTEXT

  FOR EACH cKey IN hImages:Keys
      cHtml := StrTran(cHtml, "{" + cKey + "}", hImages[cKey])
   NEXT

return cHtml

Function LoadImages()
   LOCAL hImages := {=>}
    hImages["IMGHOME"]       := "data:image/png;base64," + hb_base64encode( hb_memoRead( "DASH_HOME" ) )
    RETURN hImages


 static function BuildMainBar(oWnd)
   local oBar
   DEFINE BUTTONBAR oBar OF oWnd 2015 SIZE 90, 60
   return nil


test.rc (please put your image)

Code: Select all | Expand

1 24 "WindowsXP.Manifest"
DASH_HOME       10   "dashboard/home.png"



try with

Function LoadImages()
LOCAL hImages := {=>}
LOCAL cImageData := GetResources("DASH_HOME")

IF Empty(cImageData) .OR. ValType(cImageData) != "C"
MsgInfo("Error!")
ELSE
hImages["IMGHOME"] := "data:image/png;base64," + hb_base64encode(cImageData)
ENDIF

RETURN hImages
Last edited by Silvio.Falconi on Fri Mar 28, 2025 9:33 am, edited 2 times in total.
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Antonio Linares
Site Admin
Posts: 42716
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 93 times
Been thanked: 103 times
Contact:

Re: resource on hash no run with webview2

Post by Antonio Linares »

Check from the console of the DevTools if you get any errors
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Silvio.Falconi
Posts: 7235
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

Re: resource on hash no run with webview2

Post by Silvio.Falconi »

Antonio Linares wrote: Thu Mar 27, 2025 8:03 pm Check from the console of the DevTools if you get any errors
as I told you elsewhere I need to read the png from the resources, the hb_base64encode function expects to read a code loaded from memoread that it can't do with the resource, so the png image must be extracted from the resources and converted to base 64 to be used inside the html this is because I'm not using a webserver or a website but I'm doing it all locally, the Qween Artificial Intelligence (Grok3 and chagpt only told me nonsense without helping me) suggests a function to load the resource image and convert it to base 64, this function needs a SizeofResource function that doesn't find it if I compile the procedure


Turbo Incremental Link 6.97 Copyright (c) 1997-2022 Embarcadero Technologies, Inc.
Error: Unresolved external '_HB_FUN_SIZEOFRESOURCE' referenced from C:\WORK\ERRORI\PNG_FROM_RES\OBJ\TEST.OBJ
Error: Unable to perform link
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
Posts: 7235
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

Re: resource on hash no run with webview2

Post by Silvio.Falconi »

I add these function on my source and the exe run now

add kernel32.lib

Code: Select all | Expand

#pragma BEGINDUMP

#include <windows.h>

HB_FUNC( SIZEOFRESOURCE )
{
   HMODULE hModule = (HMODULE) hb_parnl(1);
   HRSRC hResInfo = (HRSRC) hb_parnl(2);

   hb_retnl(SizeofResource(hModule, hResInfo));
}

HB_FUNC( FINDRESOURCE )
{
   HMODULE hModule = (HMODULE) hb_parnl(1);
   LPCSTR lpName = (LPCSTR) hb_parc(2);
   LPCSTR lpType = (LPCSTR) hb_parc(3);

   hb_retnl((LONG_PTR) FindResource(hModule, lpName, lpType));
}

HB_FUNC( LOADRESOURCE )
{
   HMODULE hModule = (HMODULE) hb_parnl(1);
   HRSRC hResInfo = (HRSRC) hb_parnl(2);

   hb_retnl((LONG_PTR) LoadResource(hModule, hResInfo));
}

HB_FUNC( LOCKRESOURCE )
{
   HGLOBAL hResData = (HGLOBAL) hb_parnl(1);

   hb_retnl((LONG_PTR) LockResource(hResData));
}

#pragma ENDDUMP


the resource is found, the resource is loaded but then the size is zero and it can't convert it to base 64
I tried with another file and it gives the same error, that is, it finds the resource and uploads it but then the size is always zero
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
Posts: 7235
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

Re: resource on hash no run with webview2

Post by Silvio.Falconi »

Image


the functions

Code: Select all | Expand



FUNCTION LoadResourceToBase64(cResourceName)
LOCAL cResourceData := crestostr(cResourceName, RT_RCDATA)  // Legge la risorsa come stringa
LOCAL cBase64

IF EMPTY(cResourceData)
   MsgStop("Error: Unable to read file'" + cResourceName + "'.")
   RETURN NIL
ENDIF

//MsgInfo("Resource '" + cResourceName + "' read correctly.")

// Converte i dati della risorsa in Base64
cBase64 := hb_base64encode(cResourceData, LEN(cResourceData))

IF EMPTY(cBase64)
   MsgStop("Error: Base64 conversion failed.")
   RETURN NIL
ENDIF

//MsgInfo("Base64 conversion completed.")

RETURN "data:image/png;base64," + cBase64


FUNCTION LoadImageFromFile(cFilePath)
LOCAL cFileContent := hb_MemoRead(cFilePath)
LOCAL cBase64

IF EMPTY(cFileContent)
   MsgStop("Error: Unable to read file '" + cFilePath + "'.")
   RETURN NIL
ENDIF

cBase64 := hb_base64encode(cFileContent, LEN(cFileContent))
RETURN "data:image/png;base64," + cBase64


I use the function with

Function html()
LOCAL hImages := LoadImagesToHash()
.....


FUNCTION LoadImagesToHash()
LOCAL hImages := {=>}
hImages["IMGHOME"] := LoadResourceToBase64("DASH_HOME")
RETURN hImages
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
Posts: 7235
Joined: Thu Oct 18, 2012 7:17 pm
Been thanked: 16 times

Re: resource on hash no run with webview2 - RESOLVED

Post by Silvio.Falconi »

Antonio,
better with any type of file


Code: Select all | Expand


FUNCTION LoadResourceToBase64(cResourceName, cMimeType)
LOCAL cResourceData := crestostr(cResourceName, RT_RCDATA)  // Read the resource as a string
LOCAL cBase64

IF EMPTY(cResourceData)
   MsgStop("Error: Unable to read resource '" + cResourceName + "'.")
   RETURN NIL
ENDIF

MsgInfo("Resource '" + cResourceName + "' loaded successfully.")

// Convert resource data to Base64
cBase64 := hb_base64encode(cResourceData, LEN(cResourceData))

IF EMPTY(cBase64)
   MsgStop("Error: Base64 conversion failed.")
   RETURN NIL
ENDIF

MsgInfo("Base64 conversion completed.")

// Return the Base64 string with the correct MIME type
RETURN "data:" + cMimeType + ";base64," + cBase64


use
hImages["ICON"] := LoadResourceToBase64("ICON_GIF", "image/gif")
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Post Reply