ishwara.us Think Bigger about Business Intelligence

23Jun/110

VB.NET Function: Download Image dari internet ke File

Download2suatu hari saya butuh download file image dari internet untuk diakses di komputer lokal, functionnya adalah sebagai berikut:

Public Function webDownloadImage(ByVal Url As String, Optional ByVal saveFile As Boolean = False, _


 

                                    Optional ByVal location As String = "C:\") As Image


 

       Dim webClient As New System.Net.WebClient


 

       Dim bytes() As Byte = webClient.DownloadData(Url)


 

       Dim stream As New IO.MemoryStream(bytes)


 

       If saveFile Then My.Computer.FileSystem.WriteAllBytes(Location, bytes, False)


 

       Return New System.Drawing.Bitmap(stream)


 

   End Function

 

cara pakai:

webDownloadImage("http://www.website.com/image.jpg", True, "C:\temp.jpg")

dan file image yang berada di c:\ bernama temp.jpg bisa anda olah untuk keperluan lainnya, sesuaikan parameternya sendiri sesuai kebutuhan anda :)

misal digunakan di picturebox:

Picturebox1.image = webDownloadImage("http://www.website.com/image.jpg", True, "C:\temp.jpg")

 

atau, kalau nggak mau di save ke file, mau nya stream saja, bisa pakai script ini:

PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New _


 

System.Net.WebClient().DownloadData(http://www.website.com/image.jpg)))

 

semoga berguna.


sumber: VBForum

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.

Switch to our mobile site