Using httpwebrequest to get an image from a site into bytes []

I want to read a raw PNG binary file on a website and save it in byte [], so far I have something like this:

Uri imageUri = new Uri("http://www.example.com/image.png"); // Create a HttpWebrequest object to the desired URL. HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imageUri); using (HttpWebResponse imgResponse = (HttpWebResponse)imgRequest.GetResponse()) { using (BinaryReader lxBR = new BinaryReader(imgResponse.GetResponseStream())) { using (MemoryStream lxMS = new MemoryStream()) { lnBuffer = lxBR.ReadBytes(1024); while (lnBuffer.Length > 0) { lxMS.Write(lnBuffer, 0, lnBuffer.Length); lnBuffer = lxBR.ReadBytes(1024); } lnFile = new byte[(int)lxMS.Length]; lxMS.Position = 0; lxMS.Read(lnFile, 0, lnFile.Length); } } } 

but I can’t use GetResponse for Silverlight because it is not asynchronous (I think the reason), so I should use BeginGetResponse , but I don’t quite understand how to do it. This is what I have so far:

  HttpWebResponse imgResponse = (HttpWebResponse)imgRequest.BeginGetResponse(new AsyncCallback(WebComplete), imgRequest); using (imgResponse) { using (BinaryReader lxBR = new BinaryReader(imgResponse.GetResponseStream())) { /*Same*/ } } 

and

  void WebComplete(IAsyncResult a) { HttpWebRequest req = (HttpWebRequest)a.AsyncState; HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a); //...? Do I need something else here? } 

can someone explain me a little how to use the BeginGetResponse property and how I can use AsyncCallback. Thanks!

Note: I am new to Silverlight, and I am following tutorials and borrowing from other answers here on StackOverflow: https://stackoverflow.com/a/4646/

textbook

WebRequest_in_Silverlight

is valid silverlight code?

 HttpWebResponse imgResponse = (HttpWebResponse)imgRequest.BeginGetResponse(new AsyncCallback(WebComplete), imgRequest); 
+4
source share
2 answers

I got his work, I want to publish it here if someone needs it.

I need to get this image and then change it (byte level) Silverlight does not allow me to save the image directly in WriteableBitmap , and so I needed to get the image using WebClient as a stream, and then save it in byte[]

this is how I get the image (I already have a specific Uri):

  WebClient wc = new WebClient(); wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync(uri) 

so when the image loads, the wc_OpenReadCompleted method is wc_OpenReadCompleted and it does something like this:

 int lengthInBytes = Convert.ToInt32(e.Result.Length); BinaryReader br = new BinaryReader(e.Result); byte[] buffer = new byte[lengthInBytes]; using (br) { for (int i = 0; i < lengthInBytes; i++) { buffer[i] = br.ReadByte(); } } 

at the end, buffer[] has all the bytes of the image (what I wanted) I'm sure there are better ways to do this, but it works for me! )

note: at some point, I need to convert byte [] to BitmapImage (this was easier than expected):

  //imageInBytes is a byte[] if (imageInBytes != null) { MemoryStream rawBytesStream = new MemoryStream(imageInBytes); BitmapImage img = new BitmapImage(); img.SetSource(rawBytesStream); return img; } 

Hope this helps anyone.

+4
source

Use the OpenReadAsync method of the OpenReadAsync object. Join the OpenReadCompleted WebClient event. Use the Stream provided by the Result property of the event arguments.

Consider setting AllowReadStreamBuffering , it will fill the entire stream before raising OpenReadCompleted . In any case, most likely, you can use this thread to perform your real task, and not to deal with it in a MemoryStream.

+1
source

All Articles