Pull Image into Byte Array from a URL


/ Published in: C#
Save to your folder(s)

This snippet pulls down an image from a URL into a byte(). From there you can re-size or save.

Don't forget the necessary try - catch.


Copy this code and paste it in your HTML
  1. public byte[] getImageFromUrl(string url)
  2. {
  3. System.Net.HttpWebRequest request = null;
  4. System.Net.HttpWebResponse response = null;
  5. byte[] b = null;
  6.  
  7. request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
  8. response = (System.Net.HttpWebResponse)request.GetResponse();
  9.  
  10. if (request.HaveResponse)
  11. {
  12. if (response.StatusCode == System.Net.HttpStatusCode.OK)
  13. {
  14. Stream receiveStream = response.GetResponseStream();
  15. using (BinaryReader br = new BinaryReader(receiveStream))
  16. {
  17. b = br.ReadBytes(500000);
  18. br.Close();
  19. }
  20. }
  21. }
  22.  
  23. return b;
  24. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.