Jump to content

[SOLVED] Load image from URL


johnmcloud
 Share

Recommended Posts

Hi guys, i have a little problem with a $var of this script, i don't know how to make it:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <String.au3>
#include <File.au3>

Global $image

$GUI = GUICreate("Test GUI", 466, 360, -1, -1)
$InfoButton = GUICtrlCreateButton("Info", 14, 312, 123, 25)
$WebPic = GUICtrlCreatePic(_GetURLImage('"' & $image & '"'), 16, 216, 120, 90)
If @error = 1 Then
$GroupPic = GUICtrlCreateGroup("", 15, 210, 120, 100)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $InfoButton
   Read_Info()
EndSwitch
WEnd

Func Read_Info()
Dim $logfile = @WorkingDir & "\test.txt"
If FileGetSize($logfile) <> 0 Then
  Global $image = FileReadLine($logfile, 2)
  MsgBox(0,0, FileReadLine($logfile, 2))
EndIf
EndFunc   ;==>Read_Info

Func _GetURLImage($sURL, $sDirectory = @TempDir)
Local $hDownload, $sFile
$sFile = StringRegExpReplace($sURL, "^.*/", "")
If @error Then
  Return SetError(1, 0, $sFile)
EndIf
If StringRight($sDirectory, 1) <> "\" Then
  $sDirectory = $sDirectory & "\"
EndIf
$sDirectory = $sDirectory & $sFile
If FileExists($sDirectory) Then
  Return $sDirectory
EndIf
$hDownload = InetGet($sURL, $sDirectory, 17, 1)
While InetGetInfo($hDownload, 2) = 0
  If InetGetInfo($hDownload, 4) <> 0 Then
   InetClose($hDownload)
   Return SetError(1, 0, $sDirectory)
  EndIf
  Sleep(105)
WEnd
InetClose($hDownload)
Return $sDirectory
EndFunc   ;==>_GetURLImage

The Test.txt

blabla
dizziness-and-balance.com/images/eye-chart.jpg
blabla

I have removed the http://www. on the forum, but on the file the link is full

On MsgBox or ConsoleWrite i see the full link ( the link posted change everytime, so i can't make a direct link to the image on PC ) but the image is not loaded on GUI.

What is the problem?

Thanks for support :)

Edited by johnmcloud
Link to comment
Share on other sites

I think it's because you are trying to load an image from the web.

Try download it first using the INet functions and save it somewhere local.

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Thanks AlmarM, but i don't think is the problem, because _GetUrlImage() download the image on PC and give me the directory position, in @Tempdir &amp; "\nameofthefile.jpeg"

Ah, I oversaw that.

Whats the reason you are adding a " twice in the _GetURLImage?

$WebPic = GUICtrlCreatePic(_GetURLImage('"' & $image & '"'), 16, 216, 120, 90)

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Because i can't make it without Posted Image

GetImageUrl("http://qwerty") - work

GetImageUrl(http://qwerty) - not work

My problem is set the $var, with link work, with the variable not, but the consolewrite give me the right url. Try it.

First of all, remove those 2 ". It's no use since you are using a variable.

Second, the link inside the text.txt is correct, but the link provided by the _GetURLImage returns only @TempDir.

Therefore the image will not show because the _GetURLImage doesn't return the filename + extension.

MsgBox(0, "", _GetURLImage($image))

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Guys, maybe i'm not so clear, my problem is not with the script, the script work file, take a look:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <String.au3>
#include <File.au3>

$GUI = GUICreate("Test GUI", 466, 360, -1, -1)
$InfoButton = GUICtrlCreateButton("Info", 14, 312, 123, 25)
$WebPic = GUICtrlCreatePic(_GetURLImage("http://www.autoitscript.com/forum/uploads/profile/photo-thumb-29844.jpg"), 16, 216, 120, 90)
If @error = 1 Then
$GroupPic = GUICtrlCreateGroup("", 15, 210, 120, 100)
EndIf
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
EndSwitch
WEnd

Func _GetURLImage($sURL, $sDirectory = @TempDir)
Local $hDownload, $sFile
$sFile = StringRegExpReplace($sURL, "^.*/", "")
If @error Then
  Return SetError(1, 0, $sFile)
EndIf
If StringRight($sDirectory, 1) <> "" Then
  $sDirectory = $sDirectory & ""
EndIf
$sDirectory = $sDirectory & $sFile
If FileExists($sDirectory) Then
  Return $sDirectory
EndIf
$hDownload = InetGet($sURL, $sDirectory, 17, 1)
While InetGetInfo($hDownload, 2) = 0
  If InetGetInfo($hDownload, 4) <> 0 Then
   InetClose($hDownload)
   Return SetError(1, 0, $sDirectory)
  EndIf
  Sleep(105)
WEnd
InetClose($hDownload)
Return $sDirectory
EndFunc   ;==>_GetURLImage

Oh, @AlmarM, The func _GetURLImage give me the full directory + extension:

ConsoleWrite(_GetURLImage("http://www.autoitscript.com/forum/uploads/profile/photo-thumb-29844.jpg")

Result is:

C:DOCUME~1WINDOW~1IMPOST~1Tempphoto-thumb-29844.jpg

For the "" you have right, it's useless but the result is the same. I'll remove them

Resume, i want to do this:

1) Open the script, no image ( i have did it with if @error = 1 )

2) Click on info button --> _GetURLImage --> load image into GUI

So my problem is i don't know how to do the 2), i need help for this. Thanks to all

Edited by johnmcloud
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...