Download image and create bitmap

I am trying to download an image from a website and create a bitmap based on this image. It looks like this:

public void test() { PostWebClient client = new PostWebClient(callback); cookieContainer = new CookieContainer(); client.cookies = cookieContainer; client.download(new Uri("SITE")); } public void callback(bool error, string res) { byte[] byteArray = UnicodeEncoding.UTF8.GetBytes(res); MemoryStream stream = new MemoryStream( byteArray ); var tmp = new BitmapImage(); tmp.SetSource(stream); } 

I get "Unspecified error" in the last line of the callback method. An interesting fact is that if I use BitmapImage (new Uri ("SITE")), it works well ... (I cannot do this because I want to capture cookies from this URL. Image is jpg . PostWebClient Class -> http://paste.org/53413

+7
source share
4 answers

This is the simplest code from the Bitmap class documentation.

  System.Net.WebRequest request = System.Net.WebRequest.Create( "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif"); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); Bitmap bitmap2 = new Bitmap(responseStream); 

MSDN link for bitmap

+18
source

The easiest way is to open the network stream through an instance of WebClient and pass it the constructor of the Bitmap class:

 using (WebClient wc = new WebClient()) { using (Stream s = wc.OpenRead("http://hell.com/leaders/cthulhu.jpg")) { using (Bitmap bmp = new Bitmap(s)) { bmp.Save("C:\\temp\\octopus.jpg"); } } } 
+4
source

You can try under the code:

  private Bitmap LoadPicture(string url) { HttpWebRequest wreq; HttpWebResponse wresp; Stream mystream; Bitmap bmp; bmp = null; mystream = null; wresp = null; try { wreq = (HttpWebRequest)WebRequest.Create(url); wreq.AllowWriteStreamBuffering = true; wresp = (HttpWebResponse)wreq.GetResponse(); if ((mystream = wresp.GetResponseStream()) != null) bmp = new Bitmap(mystream); } finally { if (mystream != null) mystream.Close(); if (wresp != null) wresp.Close(); } return (bmp); } 
+3
source

try the following:

  string url ="http://www.google.ru/images/srpr/logo11w.png" PictureBox picbox = new PictureBox(); picbox.Load(url); Bitmap bitmapRemote = (Bitmap) picbox.Image; 

url is an image on the Internet, we create a new instance of the PictureBox, and then we call the NOT ASYNC procedure for loading the image from the URL when the received image receives the image as a raster image. You can also use Threading to work with the form, invoke loading in another thread, and pass the delete method to get the image upon completion.

0
source

All Articles