The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Reading An Image from the web...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Duncan Mackenzie

Posts: 689
Nickname: duncanma
Registered: Aug, 2003

Duncan Mackenzie is the Visual Basic Content Strategist at msdn.microsoft.com
Reading An Image from the web... Posted: Nov 17, 2003 12:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Duncan Mackenzie.
Original Post: Reading An Image from the web...
Feed Title: Code/Tea/Etc...
Feed URL: /msdnerror.htm?aspxerrorpath=/duncanma/rss.aspx
Feed Description: Duncan is the Visual Basic Content Strategist at MSDN, the editor of the Visual Basic Developer Center (http://msdn.microsoft.com/vbasic), and the author of the "Coding 4 Fun" column on MSDN (http://msdn.microsoft.com/vbasic/using/columns/code4fun/default.aspx). While typically Visual Basic focused, his blogs sometimes wanders off of the technical path and into various musing of his troubled mind.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Duncan Mackenzie
Latest Posts From Code/Tea/Etc...

Advertisement

Nothing amazingly difficult about this task, but it was an interesting GotDotNet question posted today so I thought I would answer it here;

Glenn Holden asks how to turn this file based function into one for images stored at http addresses...
Protected Shared Function GetImageFromFile(ByVal FileName As String) As Byte()
    Dim myFile As String = FileName
    Dim fs As FileStream = New FileStream(myFile, FileMode.Open, FileAccess.Read)
    Dim br As BinaryReader = New BinaryReader(fs)
    Dim bytesize As Long = fs.Length
    ReDim GetImageFromFile(bytesize)
    GetImageFromFile = br.ReadBytes(bytesize)
End Function
So, I produced this;
Function GetImageFromURL(ByVal url As String) As Byte()
    Dim wr As HttpWebRequest = _
       DirectCast(WebRequest.Create(url), HttpWebRequest)
    Dim wresponse As HttpWebResponse = _
       DirectCast(wr.GetResponse, HttpWebResponse)
    Dim responseStream As Stream = wresponse.GetResponseStream
    Dim br As BinaryReader = New BinaryReader(responseStream)
    Dim bytesize As Long = wresponse.ContentLength
    Return br.ReadBytes(bytesize)
End Function
with a bit of test code thrown into a button.....
Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim img As New Bitmap( _
       New IO.MemoryStream( _
        GetImageFromURL( _
        "http://msdn.microsoft.com/longhorn/art/codenameLonghorn.JPG") _
        ))
    Me.BackgroundImage = img
End Sub
A generalized solution that will accept file paths or URIs and automatically determine how to retrieve the stream would likely be useful, but I think this will do for Glenn...

Markup provided by Darren Neimke's cool markup sample from MSDN

(Listening To: Pets [Porno For Pyros / Big Shiny 90's])

Read: Reading An Image from the web...

Topic: Geek Notes 2003-11-13 Previous Topic   Next Topic Topic: C# Performance

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use