silverlight - Using httpwebrequest to get image from website to byte[] -


I want to read raw binary of a PNG file on a website and want to store it in a byte [], now Until I have something like this:

  Uri image URI = new Uri ("http://www.example.com/image.png"); // Create a HTTP Visible Object for the desired URL. HttpWebRequest imgRequest = (HttpWebRequest) WebRequest.Create (imageUri); (Using memorystream LXMS = new memorystream ()) {LNBFar = LxBr. ReadBytes (1024) (HTTPBib Resigns IMIRPSPonstation = (HTTP Web Response) (IMGR FAQ) Gate Response () (Use (BinaryRadder LXBR = New Binary Reader (IMGRPS); while (lnBuffer.Length & gt; 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 not use GetResponse on Silverlight because it is asynchronous (I think That is not the reason) rather than instead I should use initial response response , but I am not completely clear how to go about it. This is what I have so far:

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

and

  zero WebComplete (IAsyncResult A) {HttpWebRequest req = (HttpWebRequest) a.AsyncState; HttpWebResponse Res = (HttpWebResponse) req.EndGetResponse (A); // ...? Do I need anything else here? }  

Can anyone tell me how to use BeginGetResponse property and how do I use ASCII callback? ! Thanks

Note: I am new to Silverlight and I have been following tutorials and borrowing from other responses here on StackOverflow:

This valid Silverlight code

  HttpWebResponse imgResponse = (HttpWebResponse) imgRequest.BeginGetResponse (New AsyncCallback (WebComplete), imgRequest);  

This is working, want to post it here, if anyone the wanted.

I need to get this image and then modify it (byte level) Silverlight did not allow me to save the image directly in WriteableBitmap and In this way save me the image as a stream with a weblink and then save it to a byte []

, I get an image like this (I have Is already specific):

  webclient wc = new webclient (); Wc.OpenReadCompleted + = New OpenRed CompletedEventHandler (wc_OpenReadCompleted); Wc.OpenReadAsync (URI)  

So when the image is full, the wc_OpenReadCompleted method is called and it is something like this:

 < Code> Integer length InBytes = Convert.ToInt32 (e.Result.Length); Binaryreader BR = New Binary Reader (E. Results); Byte [] buffer = new byte [lengthite]; Use (br) {for (int i = 0; i & lt; lengthbibets; i ++) {buffer [i] = br.ReadByte (); Finally}  buffer []  are all bytes of the image (what I want) I'm sure there are better ways to do this but it works for me Has been doing! ) 

Note: At some point I need to convert the byte [] to bitmap image (it was easier than expected):

  // Image Inbits one Byte [] if (imageInBytes! = Null) {MemoryStream rawBytesStream = New MemoryStream (imageInBytes); Bitmap image img = new bitmap image (); Img.SetSource (rawBytesStream); Return img; }  

I hope this helps someone.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -