Loadimage_net()

Share your advanced PureBasic knowledge/code with the community.
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Loadimage_net()

Post by dobro »

something stupid, who often asked me problem

when want to share a Graphic code on the forum, you say
"I would love to find a picture on the computer, which is in everyone"

so has to be that the example we share, although using the same image for all ....

then different ways to offer us

1 - include the image in Data .... hum that increases the code ...
2 - make an archive (zip) containing the picture ... it force have an accommodation somewhere to upload the code

3 - Gling! gling! (yes it's like that in my head when I find a lousy idea: lol:

use a picture available on the NET .... ha yeah,
but .... no luck Purebasic do not have a function for loading an image from the Net!

good no problem .... we will make it :)

Code: Select all

Procedure.l Loadimage_net(Adr$,nom_image$)
	; By Dobro
	;Adr$=adresse du fichier image a charger
	;nom_image$ = nom qu'on donne a cette image
	; la procedure renvoi l'Id de l'image recupéré
	InitNetwork()
	If ReceiveHTTPFile(Adr$, GetTemporaryDirectory()+nom_image$) 
		imagId=LoadImage(#PB_Any, GetTemporaryDirectory()+nom_image$)
		DeleteFile(GetTemporaryDirectory()+nom_image$)
		Debug "image récupérée !"
		ProcedureReturn imagId
		Else
		Debug "Erreur"
	EndIf
EndProcedure


and behold, the procedure in action, if it works you should see a cat at home ....
now, we can make Graphic codes using an image that everyone
will see ....

Code: Select all

UseJPEGImageDecoder()
Declare.l Loadimage_net(Adr$,nom_image$)


Enumeration
	#win
	#screen
Endenumeration

InitSprite()
InitMouse()
InitKeyboard()

Global image

Adr$="http://lapeniche.net/wp-content/uploads/2013/12/Chat.jpg" ; image a afficher qui viens du Net
nom_image$="Chat.jpg"

image=Loadimage_net(Adr$,nom_image$)




if OpenWindow(#win,10,10,1024,768,"test")
	if OpenWindowedScreen(WindowID(#win),1,1,1024,768,1,1,1)
		startdrawing(screenoutput())
			if IsImage(image)<>0
				ResizeImage(image,1024,768) ; pour faire entrer dans la fenetre
				DrawImage(imageId(image),1,1)
			Endif
		Stopdrawing()
		FlipBuffers() 
	Endif
Endif


;-boucle principale
Repeat
	Repeat ; beurk !
		Event = WaitWindowEvent(2)
		
		Select Event 
			Case #PB_Event_Gadget
			If EventGadget() = 0
				End
			EndIf
			
			Case #PB_Event_CloseWindow
			End 
		EndSelect
	Until Event = 0
	
	FlipBuffers() 
	;ClearScreen(RGB(0, 0, 0))
	
ForEver


;-zone procedure
Procedure.l Loadimage_net(Adr$,nom_image$)
	; By Dobro
	;Adr$=adresse du fichier image a charger
	;nom_image$ = nom qu'on donne a cette image
	; la procedure renvoi l'Id de l'image recupéré
	InitNetwork()
	If ReceiveHTTPFile(Adr$, GetTemporaryDirectory()+nom_image$) 
		imagId=LoadImage(#PB_Any, GetTemporaryDirectory()+nom_image$)
		DeleteFile(GetTemporaryDirectory()+nom_image$)
		Debug "image récupérée !"
		ProcedureReturn imagId
		Else
		Debug "Erreur"
	EndIf
EndProcedure
; Epb

it's so simple, I wonder why Fred did not include this possibility in Purebasic

what? Ha .... yes, it is too simple to be thought of! :lol: :)
Last edited by dobro on Fri Jun 13, 2014 8:28 am, edited 2 times in total.
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Loadimage_net()

Post by luis »

Hi, it has been done countless times before in posts from the trick and tips section (not being the trick, just supporting it) for exact the same reason :wink:

http://www.purebasic.fr/english/viewtop ... 88#p375988
http://www.purebasic.fr/english/viewtop ... 64#p270564
http://www.purebasic.fr/english/viewtop ... 43#p416443
... etc ...

Never encountered before ? :shock:
"Have you tried turning it off and on again ?"
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Loadimage_net()

Post by dobro »

luis wrote: Never encountered before ? :shock:
Well ... No :)

the examples you give, effectively using the principle

but no one has thought made ​​a Procedure for a simplified use

this instruction should be part of Purebasic ....

if you look good in almost all the "useful" codes
are contained in other listing ... so scattered ..

so that can I be seen, these portions of code without actually doing relationship ...
with the need to use the same image for all purposes of sharing examples ...

my goal was, to create a function to load image on the Net
lack of function to do this, I appeared to be a lack ...

if I push your reasoning to the end
a lot of code on the forums, which contain animate a sprite
but not many codes, offers a dedicated function has the sprite animation ;)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Loadimage_net()

Post by luis »

EDIT: on second thought: :lol:

Maybe someone will implement this as a module, no one thought of it.
"Have you tried turning it off and on again ?"
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadimage_net()

Post by heartbone »

dobro wrote:something stupid, who often asked me problem

when want to share a Graphic code on the forum, you say
"I would love to find a picture on the computer, which is in everyone"

so has to be that the example we share, although using the same image for all ....

then different ways to offer us

1 - include the image in Data .... hum that increases the code ...
2 - make an archive (zip) containing the picture ... it force have an accommodation somewhere to upload the code

3 - Gling! gling! (yes it's like that in my head when I find a lousy idea: lol:

use a picture available on the NET .... ha yeah,
but .... no luck Purebasic do not have a function for loading an image from the Net!

good no problem .... we will make it :)
{snip}
it's so simple, I wonder why Fred did not include this possibility in Purebasic

what? Ha .... yes, it is too simple to be thought of! :lol: :)
I was just thinking about something like this yesterday and you have provided a simple solution today.
I was considering locating and using a picture that *should* be on the user's computer, but yours is a far better solution.
A good alternative to the lack of attachments in these posts.

Thanks dobro, I will be using this procedure to demonstrate the rampant Linux graphics bugs which need vanquishing.
Keep it BASIC.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Loadimage_net()

Post by VB6_to_PBx »

Code: Select all

UseJPEGImageDecoder()
Declare.l Loadimage_net(Adr$,nom_image$)


Enumeration
   #win
   #screen
Endenumeration

InitSprite()
InitMouse()
InitKeyboard()

global Dim element.element(1)
Global image

Adr$="http://lapeniche.net/wp-content/uploads/2013/12/Chat.jpg" ; image a afficher qui viens du Net
nom_image$="Chat.jpg"

image=Loadimage_net(Adr$,nom_image$)

global Dim element.element(1)

should be-->

global Dim element(1)


???
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Loadimage_net()

Post by fsw »

Or even better:

delete it altogether,
as it's not used anywhere...

:shock:

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Loadimage_net()

Post by netmaestro »

Code: Select all

imagId=LoadImage(#PB_Any, "c:\"+nom_image$)
This won't work on Windows 8 as it won't let you save a file directly to the c: drive. Consider using GetTemporaryDirectory() which is the appropriate place for it anyway.
BERESHEIT
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Loadimage_net()

Post by dobro »

VB6_to_PBx wrote: global Dim element.element(1)

should be-->

global Dim element(1)


???
sorry , it is a reliqua another code ... must be deleted :)

@netmaestro :
yes, you're absolutely right ...
personally, I use Seven, and it works ..

but I modified ....


glad not to have 8;)


@luis : or an Macro :lol:

Thanks All :)
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadimage_net()

Post by heartbone »

Slightly tweaked.

Code: Select all

UseJPEGImageDecoder() : UsePNGImageDecoder()
InitSprite() : InitKeyboard() : InitNetwork()

Procedure LOADNETIMAGE(WADDY$,IMGNAME$)
   IMAGEID= 0
   If ReceiveHTTPFile(WADDY$,IMGNAME$)
      IMAGEID= LoadImage(#PB_Any,IMGNAME$)
      DeleteFile(IMGNAME$)
   EndIf
   ProcedureReturn IMAGEID
EndProcedure

URL$="http://i.dailymail.co.uk/i/pix/2012/11/06/article-0-15E0AB62000005DC-563_634x421.jpg"
IMG1= LOADNETIMAGE(URL$,"rat.jpg")
URL$= "http://www.thelookingspoon.com/tlsimages/blog/2010/obama_face.png"
IMG2= LOADNETIMAGE(URL$,"face.png")

If OpenWindow(0,100,100,800,600,"POTUS unsuited")
   If OpenWindowedScreen(WindowID(0),0,0,800,600,1,0,0)
      If IsImage(IMG2)
         StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent)
         DrawImage(ImageID(IMG2),0,0) : StopDrawing()
         GrabSprite(1,0,0,287,380,#PB_Sprite_AlphaBlending)
         TransparentSpriteColor(1,$FFFFFF) 
         If IsImage(IMG1)
            ResizeImage(IMG1,800,600) 
            For I= 1 To 2
               StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent)
               DrawImage(ImageID(IMG1),0,0) : StopDrawing()
               DisplayTransparentSprite(1,258,115) : FlipBuffers()
            Next I
         EndIf
      EndIf
   EndIf
EndIf

Repeat
   Delay(2) : ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : End : EndIf
   Repeat : X= WindowEvent() : If X = #PB_Event_CloseWindow : End : EndIf   
   Until X = 0   
ForEver
Or is it tightly squeaked?
heartbone wrote:Thanks dobro, I will be using this procedure to demonstrate the rampant Linux graphics bugs which need vanquishing.
If I am not mistaken the above snippet should better expose the problem with the bugged Linux TransparentSpriteColor(NUM,$FFFFFF) command.
Keep it BASIC.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Loadimage_net()

Post by heartbone »

Because of the substantial differences in the way that graphics are processed in Linux compared to within Windows®,
I had to retweak the flow of the tweaked program in order to compensate for those differences,
so now this demonstration of a graphics bug will make sense to Linux users. ;)

Code: Select all

UseJPEGImageDecoder() : UsePNGImageDecoder()
InitSprite() : InitKeyboard() : InitNetwork()

Procedure LOADNETIMAGE(WADDY$,IMGNAME$)
   IMAGEID= 0
   If ReceiveHTTPFile(WADDY$,IMGNAME$)
      IMAGEID= LoadImage(#PB_Any,IMGNAME$)
      DeleteFile(IMGNAME$)
   EndIf
   ProcedureReturn IMAGEID
EndProcedure

URL$="http://i.dailymail.co.uk/i/pix/2012/11/06/article-0-15E0AB62000005DC-563_634x421.jpg"
IMG1= LOADNETIMAGE(URL$,"rat.jpg")
URL$= "http://www.thelookingspoon.com/tlsimages/blog/2010/obama_face.png"
IMG2= LOADNETIMAGE(URL$,"face.png")

If OpenWindow(0,100,100,800,600,"POTUS unsuited")
   If OpenWindowedScreen(WindowID(0),0,0,800,600,1,0,0)
      If IsImage(IMG2)
         StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent)
         DrawImage(ImageID(IMG2),0,0) : StopDrawing()
         GrabSprite(1,0,0,287,380,#PB_Sprite_AlphaBlending)
         TransparentSpriteColor(1,$FFFFFF)
         If IsImage(IMG1)
            ResizeImage(IMG1,800,600)
            Repeat
               Delay(2) : ExamineKeyboard() : If KeyboardPushed(#PB_Key_Escape) : End : EndIf
               Repeat : X= WindowEvent() : If X = #PB_Event_CloseWindow : End : EndIf   
               Until X = 0   
               StartDrawing(ScreenOutput()) : DrawingMode(#PB_2DDrawing_Transparent)
               DrawImage(ImageID(IMG1),0,0) : StopDrawing()
               DisplayTransparentSprite(1,258,115) : FlipBuffers()
            ForEver
         EndIf
      EndIf
   EndIf
EndIf
Keep it BASIC.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Loadimage_net()

Post by idle »

@heartbone

yes there's a bug in transparency on linux but please don't hijack threads.
editing your previous post would have been a better option.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Loadimage_net()

Post by Bisonte »

And here a modified "ReceiveHTTPMemory()" from ts-soft, so no need of files to write.
... the example is now without Screen... a CanvasGadget is much better ;)

Code: Select all

;:- LoadWebImage
;:- Modified "ReceiveHTTPMemory()" from ts-soft
;:- Example by heartbone
;:- George Bisonte
;:- PB 5.22 LTS (All OS)

EnableExplicit

Procedure.i LoadWebImage(Image, Url.s, BufferSize = 4096, Timeout = 5000, Port = 80)
  
  If Not InitNetwork() : ProcedureReturn #False : EndIf
  
  Protected Connection, Time, Time2, Event, Size = 1, Size2, SizeAll, pos, ID
  Protected.s Server
  Protected *Mem, *Buffer, *Mem2
  
  If LCase(Left(URL, 7)) <> "http://" : URL = "http://" + URL : EndIf
  Server = GetURLPart(URL, #PB_URL_Site)
  If Server = "" : ProcedureReturn #False : EndIf
  Connection = OpenNetworkConnection(Server, Port, #PB_Network_TCP)
  If Not Connection : ProcedureReturn #False : EndIf
  If BufferSize <= 0 : BufferSize = 4096 : EndIf
  *Buffer = AllocateMemory(BufferSize)
  If Not *Buffer : ProcedureReturn #False : EndIf
  
  SendNetworkString(Connection, "GET " + URL + " HTTP/1.0" + #CRLF$ + #CRLF$)
  Time = ElapsedMilliseconds()
  Repeat
    Event = NetworkClientEvent(Connection)
    If Event = #PB_NetworkEvent_Data
      Repeat
        Size = ReceiveNetworkData(Connection, *Buffer, BufferSize)
        If Size > 0
          Time = ElapsedMilliseconds()
          SizeAll + Size
          *Mem = ReAllocateMemory(*Mem, SizeAll)
          If *Mem
            CopyMemory(*Buffer, *Mem + (SizeAll - Size), Size)
          Else
            CloseNetworkConnection(Connection)
            FreeMemory(*Buffer)
            ProcedureReturn #False
          EndIf
        EndIf
      Until Size <= 0
    EndIf
    Time2 = ElapsedMilliseconds() - Time
  Until Time2 > Timeout Or Size <= 0
  CloseNetworkConnection(Connection)
  FreeMemory(*Buffer)
  If Time2 > Timeout
    If *Mem : FreeMemory(*Mem) : EndIf
    ProcedureReturn #False
  EndIf
  pos = FindString(PeekS(*mem, -1, #PB_UTF8), #CRLF$ + #CRLF$, 1) - 1
  pos = Len(#CRLF$ + #CRLF$) + pos
  Size2 = MemorySize(*Mem) - pos
  *Mem2 = AllocateMemory(Size2)
  If *Mem2
    CopyMemory(*Mem + pos, *Mem2, Size2)
    FreeMemory(*Mem)
    ID = CatchImage(Image, *Mem2)
    FreeMemory(*Mem2)
    ProcedureReturn ID
  EndIf
  FreeMemory(*Mem)
  ProcedureReturn #False
  
EndProcedure

UseJPEGImageDecoder()
UsePNGImageDecoder()

Define IMG1, IMG2, Event

OpenWindow(0, 0, 0, 800, 600, "POTUS unsuited", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)

IMG1= LoadWebImage(#PB_Any, "http://i.dailymail.co.uk/i/pix/2012/11/06/article-0-15E0AB62000005DC-563_634x421.jpg")
IMG2= LoadWebImage(#PB_Any, "http://www.thelookingspoon.com/tlsimages/blog/2010/obama_face.png")

CanvasGadget(0, 0, 0, 800, 600)

If StartDrawing(CanvasOutput(0))
  
  If IsImage(IMG1)
    DrawImage(ImageID(IMG1), 0, 0, 800, 600)
  EndIf
  If IsImage(IMG2)
    DrawAlphaImage(ImageID(IMG2), 250,150, 255)
  EndIf
  StopDrawing()
EndIf


Repeat
   Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Only a progressbar or something like that, to show how much is loading (if it was more than one picture) have to work out, so
the user know why the program does "nothing" ....
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Loadimage_net()

Post by dobro »

and For the Module :)

although this will Purebasic has these functions in Native :)

I wanted absolutely to the writing on the disc in case we want to Keep the file;)


Code: Select all

;***********************************************
;Titre  :*loadmodule_net()
;Auteur  : Dobro
;Date  :12/07/2014
;Heure  :11:52:37
;Version Purebasic :  PureBasic 5.22 LTS (Windows - x86)
;Version de l'editeur :EPB V2.58
; Libairies necessaire : Aucune 
;***********************************************

Declare.l Loadmodule_net(Adr$,nom_module$)

InitSound()
chm$="http://michel.dobro.free.fr/Forum_pb/Outrun3.mod"


OpenWindow(0,0,0,320,200,"test",#PB_Window_SystemMenu)
TextGadget(1,10,10,320,35,"OutRun 3 ....  Wait Loading ...")
mo=Loadmodule_net(chm$,"Outrun3.mod"):PlayMusic(mo)
Repeat 
	Event = waitWindowEvent(2) 
Until Event =   #PB_Event_CloseWindow


Procedure.l Loadmodule_net(Adr$,nom_module$)
	; By Dobro
	;Adr$=adresse du fichier module a charger
	;nom_module$ = nom qu'on donne a cette image
	; la procedure renvoi l'Id de l'image recupéré
	InitNetwork()
	If ReceiveHTTPFile(Adr$, GetTemporaryDirectory()+nom_module$)
		id_module=LoadMusic(#PB_Any, GetTemporaryDirectory()+nom_module$)
		
		DeleteFile(GetTemporaryDirectory()+nom_module$)
		Debug "Module récupére !"
		ProcedureReturn id_module
		Else
		Debug "Erreur"
	EndIf
EndProcedure
; Epb

Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Loadimage_net()

Post by Bisonte »

dobro wrote:I wanted absolutely to the writing on the disc in case we want to Keep the file;)
To realize this, we have : SaveImage() ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply