共用方式為


Reading An Image from the web...

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( _

        "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])

Comments

  • Anonymous
    November 16, 2003
    The comment has been removed
  • Anonymous
    November 16, 2003

    .... oooh yeh, I almost forgot, thanks for posting the useful code-snippet ;-)

  • Anonymous
    November 24, 2003
    I have a trick question that is somewhat (well, fringly) related. Given a clean .Net 2003 Windows Application with:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.BackgroundImage = New Bitmap("c:test.jpg")
    End Sub

    I see a form with a beautiful background. But if I load the graphic, serialize it, and convert it back I get a beautiful memory error:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' stub test code to load an image into memory and create a bitmap from it
    Try
    ' load the graphic into memory, store it in a string
    Dim memoryStream As New System.IO.MemoryStream
    System.Drawing.Image.FromFile("C:test.jpg").Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim Buffer As Byte() = memoryStream.GetBuffer()
    Dim picStr As String = Convert.ToBase64String(Buffer)
    memoryStream.Close()

    ' convert the string back into a graphic
    Dim memoryStream2 As New System.IO.MemoryStream
    Dim Buffer2 As Byte() = Convert.FromBase64String(picStr)
    memoryStream2 = New System.IO.MemoryStream(Buffer2)
    Dim icon As System.Drawing.Image
    icon = System.Drawing.Image.FromStream(memoryStream2)
    memoryStream2.Close()

    Me.BackgroundImage = icon
    Catch ex As Exception
    Console.WriteLine(ex.ToString)
    End Try

    End Sub

    Give it a try, it's fun.
  • Anonymous
    December 29, 2003
    Mark Winder: Your problem is that Image.FromStream does NOT copy the stream contents. In the documentation for this method there's this 'remark':

    You must keep the stream open for the lifetime of the Image object.


    So you need to make memoryStream2 survive until the form is unloaded.
  • Anonymous
    December 29, 2003
    The comment has been removed
  • Anonymous
    February 02, 2004
    related to his article, may i ask how to convert a stream to its original format, say image.Because what i receive is a stream, i nedd to convert it again to for example, if its an image, i need to convert it agian in an image?Any ideas?and then i'll email that image..Tnx in advance
  • Anonymous
    February 06, 2004
    angela, note that ReadImageFromFile is actually not specific to images. It just returns a Byte array containing the content of a file.

    You need the opposite: open a file with a BinaryWriter and use the .WriteTo method of the given MemoryStream.