C# - Load Web Image Into PictureBox

This code snippet shows how to extend the PictureBox control to load images from the web using the WebClient class.
  1. using System.Collections.Generic;  
  2. using System.Drawing;  
  3. using System.IO;  
  4. using System.Windows.Forms;  
  5. using System.Net;  
  6.  
  7. class xPictureBox : PictureBox  
  8. {  
  9.     public string Url  
  10.     {  
  11.         set  
  12.         {  
  13.             using (WebClient client = new WebClient())  
  14.             {  
  15.                 byte[] bytes = client.DownloadData(value);  
  16.  
  17.                 using (MemoryStream mStream = new MemoryStream(bytes))  
  18.                 {  
  19.                     Image img = Image.FromStream(mStream);  
  20.                     this.Image = img;  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25. } 

No comments:

Post a Comment