Page 1 of 2

Loadimage_net()

Posted: Thu Jun 12, 2014 10:25 am
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: :)

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 10:47 am
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:

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 11:48 am
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 ;)

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 12:23 pm
by luis
EDIT: on second thought: :lol:

Maybe someone will implement this as a module, no one thought of it.

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 12:56 pm
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.

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 6:55 pm
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)


???

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 8:16 pm
by fsw
Or even better:

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

:shock:

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 9:09 pm
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.

Re: Loadimage_net()

Posted: Thu Jun 12, 2014 11:10 pm
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 :)

Re: Loadimage_net()

Posted: Fri Jul 11, 2014 8:44 pm
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.

Re: Loadimage_net()

Posted: Sat Jul 12, 2014 12:33 am
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

Re: Loadimage_net()

Posted: Sat Jul 12, 2014 2:02 am
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.

Re: Loadimage_net()

Posted: Sat Jul 12, 2014 10:30 am
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" ....

Re: Loadimage_net()

Posted: Sat Jul 12, 2014 10:55 am
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


Re: Loadimage_net()

Posted: Sat Jul 12, 2014 11:05 am
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() ;)