TCP- Bilder abrufen

Übersicht BlitzBasic Beginners-Corner

Neue Antwort erstellen

mk

Betreff: TCP- Bilder abrufen

BeitragMi, Mai 04, 2005 12:38
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich habe ein Bild auf meinem Arcor Webspace mit BB downloaden und anzeigen...

Bild: http://home.arcor.de/marcelkroener/Pics/dragon.jpg

BlitzBasic: [AUSKLAPPEN]

Graphics 1024,768,0,2
tcp$=OpenTCPStream (\"home.arcor.de\",80)

WriteFile (\"temp.tmp\")

temp$=OpenFile (\"temp.tmp\")


WriteLine (tcp,\"GET http://home.arcor.de/marcelkroener/Pics/dragon.jpg HTTP/1.0\")
WriteLine (tcp,\"\")

Repeat

dat$=ReadLine (tcp)bla=bla+1
If bla>10 Then WriteLine (temp,dat)
Delay 10
Until Eof (tcp)

img$=LoadImage (\"temp.tmp\")



Repeat
DrawImage img,10,10
Flip
Cls
Until KeyHit (1)

DeleteFile (\"temp.tmp\")
CloseFile temp


Ergebnis: http://home.arcor.de/marcelkroener/Pics/temp.tmp

Hab ich was falsch gemacht, oder geht das nicht mit BB? Rolling Eyes
Pentium 4 2,6 GHz | 256 MB RAM | 120 GB Festplatte | DVD-ROM Laufwerk (48 Fach) |


I am a noob Smile

www.marcelkroener.de.vu

Mein Arcor Webspace =)

Jan_

Ehemaliger Admin

BeitragMi, Mai 04, 2005 12:42
Antworten mit Zitat
Benutzer-Profile anzeigen
probiere mal, byte für byte, statt line für line.
between angels and insects
 

Apocalyptic

BeitragMi, Mai 04, 2005 13:15
Antworten mit Zitat
Benutzer-Profile anzeigen
http://www.blitzbasic.com/code...hp?code=25


BlitzBasic: [AUSKLAPPEN]
; -----------------------------------------------------------------------------
; LoadWebImage -- uses BlitzGet Deluxe, based on Mark Sibly's HTTPGet
; -----------------------------------------------------------------------------
; james@hi-toro.com
; -----------------------------------------------------------------------------

AppTitle \"LoadWebImage\"

Graphics 640, 480

SetBuffer BackBuffer ()

; -----------------------------------------------------------------------------
; Load an image from the web, straight into our game!
; -----------------------------------------------------------------------------
rocket = LoadWebImage (\"http://home.arcor.de/marcelkroener/Pics/dragon.jpg\")

; rocket = LoadWebImage (\"http://www.deathfrombeyondthestars.freeserve.co.uk/rocket.bmp\") ; Alternative URL in case of failure, as happened last night...!

; -----------------------------------------------------------------------------
; Check for failure
; -----------------------------------------------------------------------------
If rocket = 0

RuntimeError \"Failed to load web image!\": End

; Alternative (BETTER) failure method -- use a default local image supplied with your game...
; rocket = LoadImage (\"rocket.bmp\")

EndIf

MaskImage rocket, 255, 0, 255

x = 50
y = 50

ClsColor 70, 110, 190

Repeat

Cls

If KeyDown (203) = 1 Then x = x - 1
If KeyDown (205) = 1 Then x = x + 1
If KeyDown (200) = 1 Then y = y - 1
If KeyDown (208) = 1 Then y = y + 1

DrawImage rocket, x, y

Flip

Until KeyDown (1) = 1

End

Function LoadWebImage (webFile$)
If BlitzGet (webFile$, CurrentDir (), \"temp_web_image.bmp\")
image = LoadImage (\"temp_web_image.bmp\")
DeleteFile \"temp_web_image.bmp\"
EndIf
Return image
End Function

Function BlitzGet (webFile$, saveDir$, saveFile$)

; -------------------------------------------------------------------------
; Strip \"http://\" if provided
; -------------------------------------------------------------------------
If Left (webFile$, 7) = \"http://\" Then webFile$ = Right (webFile$, Len (webFile$) - 7)

; -------------------------------------------------------------------------
; Split into hostname and path/filename to download
; -------------------------------------------------------------------------
slash = Instr (webFile$, \"/\")
If slash
webHost$ = Left (webFile$, slash - 1)
webFile$ = Right (webFile$, Len (webFile$) - slash + 1)
Else
webHost$ = webFile$
webFile$ = \"/\"
EndIf

; -------------------------------------------------------------------------
; Add trailing slash to download dir if not given
; -------------------------------------------------------------------------
If Right (saveDir$, 1) <> \"\\" Then saveDir$ = saveDir$ + \"\\"

; -------------------------------------------------------------------------
; Save filename -- get from webFile$ if not provided
; -------------------------------------------------------------------------
If saveFile$ = \"\"
If webFile = \"/\"
saveFile$ = \"Unknown file.txt\"
Else
For findSlash = Len (webFile$) To 1 Step - 1
testForSlash$ = Mid (webFile$, findSlash, 1)
If testForSlash$ = \"/\"
saveFile$ = Right (webFile$, Len (webFile$) - findSlash)
Exit
EndIf
Next
If saveFile$ = \"\" Then saveFile$ = \"Unknown file.txt\"
EndIf
EndIf

; DEBUG
; RuntimeError \"Web host: \" + webHost$ + Chr (10) + \"Web file: \" + webFile$ + Chr (10) + \"Save dir: \" + saveDir$ + Chr (10) + \"Save file: \" + saveFile$

www = OpenTCPStream (webHost$, 80)

If www

WriteLine www, \"GET \" + webFile$ + \" HTTP/1.1\" ; GET / gets default page...
WriteLine www, \"Host: \" + webHost$
WriteLine www, \"User-Agent: BlitzGet Deluxe\"
WriteLine www, \"Accept: */*\"
WriteLine www, \"\"

; ---------------------------------------------------------------------
; Find blank line after header data, where the action begins...
; ---------------------------------------------------------------------

Repeat
header$ = ReadLine (www)
If Left (header$, 16) = \"Content-Length: \" ; Number of bytes to read
bytesToRead = Right (header$, Len (header$) - 16)
EndIf
Until header$ = \"\" Or (Eof (www))

If bytesToRead = 0 Then Goto skipDownLoad

; ---------------------------------------------------------------------
; Create new file to write downloaded bytes into
; ---------------------------------------------------------------------
save = WriteFile (saveDir$ + saveFile$)
If Not save Then Goto skipDownload

; ---------------------------------------------------------------------
; Incredibly complex download-to-file routine...
; ---------------------------------------------------------------------

For readWebFile = 1 To bytesToRead

If Not Eof (www) Then WriteByte save, ReadByte (www)

; Call BytesReceived with position and size every 100 bytes (slows down a LOT with smaller updates)

tReadWebFile = readWebFile
If tReadWebFile Mod 100 = 0 Then BytesReceived (readWebFile, bytesToRead)

Next

CloseFile save

; Fully downloaded?
If (readWebFile - 1) = bytesToRead
success = 1
EndIf

; Final update (so it's not rounded to nearest 100 bytes!)
BytesReceived (bytesToRead, bytesToRead)

.skipDownload
CloseTCPStream www

Else

RuntimeError \"Failed to connect\"

EndIf

Return success

End Function

; -----------------------------------------------------------------------------
; User-defined update function, called every 100 bytes of download -- alter to suit!
; -----------------------------------------------------------------------------
; TIP: Pass a user-defined type instead, with all data (this stuff plus URL, local filename, etc)
; -----------------------------------------------------------------------------
Function BytesReceived (posByte, totalBytes)
; Example update code...
Cls
Text 20, 20, \"Downloading file -- please wait...\"
Text 20, 40, \"Received: \" + posByte + \"/\" + totalBytes + \" bytes (\" + Percent (posByte, totalBytes) + \"%)\"
Flip
End Function

; -----------------------------------------------------------------------------
; Handy percentage function
; -----------------------------------------------------------------------------
Function Percent (part#, total#)
Return Int (100 * (part / total))
End Function
Suum cuique

[ www.ffs-net.de.vu ] [ Raycaster ]
 

morszeck

Betreff: Re: TCP- Bilder abrufen

BeitragMi, Mai 04, 2005 18:28
Antworten mit Zitat
Benutzer-Profile anzeigen
BlitzBasic: [AUSKLAPPEN]

Graphics 1024,768,0,2

stream_tcp = OpenTCPStream (\"home.arcor.de\",80)

stream_temp = WriteFile (\"temp.tmp\")


WriteLine ( stream_tcp, \"GET http://home.arcor.de/marcelkroener/Pics/dragon.jpg HTTP/1.0\")
WriteLine ( stream_tcp, \"\" )

While Not Eof( stream_tcp)

byte = ReadByte( stream_tcp )
WriteByte( stream_temp, byte)

Wend

CloseTCPStream stream_tcp
CloseFile stream_temp


img$=LoadImage (\"temp.tmp\")



Repeat

DrawImage img,10,10

Delay 100

Flip
Cls

Until KeyHit (1)

DeleteFile (\"temp.tmp\")
CloseFile temp

Neue Antwort erstellen


Übersicht BlitzBasic Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group