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())) { } }
and
void WebComplete(IAsyncResult a) { HttpWebRequest req = (HttpWebRequest)a.AsyncState; HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
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);
source share