From Web to image control

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: From Web to image control

Post by KDJ »

Hello friends.
I improved my example to show how to keep original aspect ratio of the image.

ImageFromWeb.jpg
ImageFromWeb.jpg (81.57 KiB) Viewed 3964 times
ImageFromWeb.zip
(3.04 KiB) Downloaded 221 times
User avatar
quartz565
Posts: 667
Joined: Mon Oct 01, 2012 12:37 pm
Location: Thessaloniki, Greece
Contact:

Re: From Web to image control

Post by quartz565 »

Another question i have is the following:

Is there a way when searching in google for an image through a browser or through an activeX window inside our program, to retrieve the image's URL?
Even if the user has to click on the first image and then on the "view image" button that google shows. Can we retrieve that last URL somehow so afterwards we can save it on the disk?

Niko
Best Regards,
Nikos.

os: Windows Server 2019 - 64
User avatar
quartz565
Posts: 667
Joined: Mon Oct 01, 2012 12:37 pm
Location: Thessaloniki, Greece
Contact:

Re: From Web to image control

Post by quartz565 »

I found this http://www.hmgforum.com/viewtopic.php?p=31675#p31675 from Claudio. I will check it.
Best Regards,
Nikos.

os: Windows Server 2019 - 64
mlnr
Posts: 126
Joined: Fri Aug 28, 2015 1:52 pm
DBs Used: DBF

Re: From Web to image control

Post by mlnr »

Nikos,

If you want to search in google, maybe use this url link.

Code: Select all

url = "https://www.google.com/search?q=" + cSearch + "&tbm=isch"
where cSearch is a variable, that you want to search.
You need to download the webpage and find the <img tag.
Please see Mol's solution. http://www.hmgforum.com/viewtopic.php?f=9&t=586

Maybe this info is helpfully.
Best regards,
Gabor
User avatar
quartz565
Posts: 667
Joined: Mon Oct 01, 2012 12:37 pm
Location: Thessaloniki, Greece
Contact:

Re: From Web to image control

Post by quartz565 »

@Gabor
Thanks a lot for the help. It's easy when you know the url to download.
The problem is to capture the images(full resolution) url when you do a search and you
essentially dont know the image that the user will select.

Niko
Best Regards,
Nikos.

os: Windows Server 2019 - 64
User avatar
vagblad
Posts: 160
Joined: Tue Jun 18, 2013 12:18 pm
DBs Used: MySQL,DBF
Location: Thessaloniki, Greece

Re: From Web to image control

Post by vagblad »

Is there a way to capture the events from the activeX control? Like hovering,push a button etc?
Vagelis Prodromidis
Email: vagblad@gmail.com, Skype: vagblad
mlnr
Posts: 126
Joined: Fri Aug 28, 2015 1:52 pm
DBs Used: DBF

Re: From Web to image control

Post by mlnr »

Nikos,

Please, use this for your search.

Code: Select all

url = "https://www.google.com/search?q=" + cSearch + "&tbm=isch"
eg. if you would like to search "car" in google pictures https://www.google.com/search?q=car&tbm=isch

1.search
2.download all the index images with image url link (maybe help Marek's code)
3.user will select
4.download full resolution images

It's just an idea...

And this is Marek's code

Code: Select all

/*
 2016.05.16 Marek Olszewski MOL 
Read WWW sample
*/


#include <hmg.ch>

Function Main

	Public cURL := "http://www.molsystemy.pl"
	// for old debugger
	SetMode(25,80)
	
	DEFINE WINDOW Main ;
        AT 0,0 ;
        WIDTH 800 HEIGHT 600 ;
        TITLE 'Read WWW sample' ;
		Main  
		
		DEFINE LABEL LABEL_1
			ROW 20
			COL 20
			WIDTH 80
			HEIGHT 18
			VALUE "Web Address"
		END LABEL

		DEFINE TEXTBOX TEXT_1
			ROW 20
			COL 110
			WIDTH 500 
			HEIGHT 24 
			VALUE cURL
			TOOLTIP 'Web Address'
		END TEXTBOX
		 
		DEFINE BUTTON B1
			ROW    45
			COL    20
			WIDTH  200
			HEIGHT 24
			Caption "Click to read www document"
			FONTNAME "Arial"
			FONTSIZE 9
			ACTION SaveURL()
		END BUTTON

		DEFINE GRID Grid_Links
			ROW 70
			COL 40
			WIDTH 700 
			HEIGHT 480
			HEADERS {'Files to download'} 
			WIDTHS {480} 
			ITEMS {}
		END GRID

	END WINDOW
	
	Center Window Main
	Activate Window Main

Return
*------------------------
function GetLinkFiles
	param cWWWFile
	
	aFilesToDownload := {}
	
	//cWWWFile := Getfile ( { {'HTML','*.html'},{'HTM','*.htm'} },'Select html file' , '.', .f. , .t. )
	
	if hb_FileExists( cWWWFile)
		cWWW := memoread(cWWWFile )
	else
		return .f.
	endif
	
	if cWWW  == NIL
		msgstop("Something wrong with "+cWWWFile + " file")
		return .f.
	endif
	
	TokenInit(@cWWW,"<>")

	do while .not. TokenEnd()
		cLink := ''
		cTag := alltrim(TokenNext(cWWW))

		if hmg_lower(cTag) = "link"
			cLink := StripTag(cTag,"href")
		
		elseif hmg_lower(cTag) = "script"
			cLink := StripTag(cTag,"src")
		
		elseif hmg_lower(cTag) = "img"
			cLink := StripTag(cTag,"src")
		
		endif
		
		if !empty(cLink)
			aAdd(aFilesToDownload, cLink)
			Main.Grid_Links.AddItem({cLink})
		endif
	enddo
	
	//msgdebug(aFilesToDownload)
return .t.
	
*-------------------
function StripTag
	param cTag, cKeyword
	local i
	local cLink
	
	i := hb_at(cKeyword, hmg_lower(cTag))
	if i > 0
		// cut path and filename
		cTag := substr(cTag,i)
		i := hb_at('"', cTag)
		cTag := substr(cTag,i+1)
		i := hb_at('"', cTag)
		cLink := left(cTag,i-1)
	endif
return cLink
*----------------------

Function SaveURL()
	
	local oHttp, cHtml, oDoc, cUrl:=alltrim(Main.Text_1.Value)
	local i
	local cBaseAddress := ''
	local cLocalFolderName
	
	private aFilesToDownload := {}
	
	oHttp := TIpClientHttp():new( cUrl )

	if.not. oHttp:open()
		MsgInfo("Connection error:", oHttp:lastErrorMessage())
		Return Nil
	endif

	cHtml := oHttp:readAll()
	oHttp:close()

	oDoc := THtmlDocument():new( cHtml )

	// create folder for saving files
	
	cLocalFolderName := dtos(date())+strtran(time(),":","")
	CreateFolder(cLocalFolderName)
	
	oDoc:writeFile( cLocalFolderName +"\"+"main.html" )

	i := rat("/", cUrl)
	//cBaseAddress := left(cUrl,i)
	cBaseAddress := cUrl + "/"
	
	GetLinkFiles(cLocalFolderName +"\"+"main.html" )
	
	for i := 1 to len(aFilesToDownload)
		if aFilesToDownload[i] = "http"
			// external links
			cFileName := aFilesToDownload[i]
			cLocalFileName :=  strtran(aFilesToDownload[i], ":","_")
			cLocalFileName :=  strtran(cLocalFileName, "/","_")
			cLocalFileName := cLocalFolderName + "\" + cLocalFileName
			
		else
			cFileName := cBaseAddress +  aFilesToDownload[i]
			cLocalFileName := aFilesToDownload[i]
			cNewFolderName := ""
			// create local nested folders
			do while .t.
				n := at("/", cLocalFileName)
				if n > 0
					cNewFolderName += left(cLocalFileName,n-1)
					CreateFolder(cLocalFolderName + "\" + cNewFolderName )
					cNewFolderName += "\"
					cLocalFileName := substr(cLocalFileName,n+1)
				else
					exit
				endif
			enddo
			cLocalFileName := cLocalFolderName + "\" + strtran(aFilesToDownload[i],"/","\")
		endif
		
		DownloadFromWWW(cFileName, cLocalFileName, .T.)
		Main.Grid_Links.AddItem({cFileName + " ---> "+cLocalFileName})
		DO Events
	next i
	
	Main.Grid_Links.AddItem({"Finished!"})
	MsgInfo("Downloaded and saved in folder: "+ cLocalFolderName)
Return Nil
*-----------------------

PROCEDURE DownloadFromWWW
	param cURL, cLocalFileName, lSilent
	LOCAL oCon, oUrl, i, cResponse
	local ret := .f.
	local OldErrorHandler
	
	OldErrorHandler := ErrorBlock({|e| break(e)})
	
	BEGIN SEQUENCE
	cLocalFileName := alltrim(cLocalFileName)
	if lSilent = NIL
		lSilent := .f.
	endif
	oUrl := tURL():New( cUrl )
	IF Empty( oUrl )
		if !lSilent
			MsgBox("Bad url " + cUrl)
		endif
		BREAK
	ENDIF

	IF oUrl:cProto != "http"
		if !lSilent
			MsgBox('Bad url')
		endif
		BREAK
	END
   
	oCon := TipClientHttp():New( oUrl )
	oCon:nConnTimeout := 20000
	if !lSilent
		MsgBox("Connecting with "+ oUrl:cServer)
	endif
	IF oCon:Open( cUrl )
		if !lSilent
			MsgBox("Connection established." +chr(10)+"Press OK to download " + oUrl:cPath +oUrl:cFile)
		endif
		oCon:WriteAll(cLocalFileName)
		if !lSilent
			MsgBox("Downloaded as: "+cLocalFileName)
		endif
		oCon:Close()
		ret := .t.
	ELSE
		IF oCon:SocketCon == NIL
			cResponse := "Connection not initialized"
		ELSEIF hb_InetErrorCode( oCon:SocketCon ) == 0
			cResponse := oCon:cReply
		ELSE
			if !lSilent
				cResponse := "Error in connection: " + hb_InetErrorDesc( oCon:SocketCon )
			endif
		ENDIF
		if !lSilent
			MsgBox("I can not connect to: "+ oUrl:cServer+chr(10)+"Server response: "+cResponse)
		endif
		ret := .f.
	END
	RECOVER
		if !lSilent
			MSGSTOP("Unknown error")
		endif
	END SEQUENCE
	ErrorBlock(OldErrorHandler)
RETURN ret
Best regards,
Gabor
User avatar
quartz565
Posts: 667
Joined: Mon Oct 01, 2012 12:37 pm
Location: Thessaloniki, Greece
Contact:

Re: From Web to image control

Post by quartz565 »

Thanks Gabor !
I will try it tomorrow.

Niko
Best Regards,
Nikos.

os: Windows Server 2019 - 64
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: From Web to image control

Post by KDJ »

Niko

1. Download source of your page to file: URLDownloadToFile("http://...", "Source.htm")
2. Read content this file into variable: cHtml := HB_MemoRead("Source.htm")
3. In cHtml find all <img> tags: <img ...>
4. Inside every tag find src parameter: <img ... src="HereIsImageAddress" ...>
5. Take into account that the address can be:
- absolute, eg. http://www.abcdefghi.com/a/b/c.jpg
- relative, eg. /a/b/c.jpg
KDJ
Posts: 243
Joined: Mon Sep 05, 2016 3:04 am
Location: Poland

Re: From Web to image control

Post by KDJ »

Example:

ImageFromWeb.png
ImageFromWeb.png (35.25 KiB) Viewed 3767 times
ImageFromWeb.zip
(4.32 KiB) Downloaded 230 times
Post Reply