c# - How to Serialise Images from Remote URL to IsolateStorage or to XML? -
i need download images remote url , serialise them isolated storage, having trouble figuring out how work, i'm serialising uris images - , can work, want download image , store in file system, best way using c# , silverlight.
i've tried finding ways this, complicated, if possible can store image data in xml if solution, want download file - .net on desktop has many methods sort of thing, need silverlight solution.
if there examples demonstrate sort of thing, may - seems straightforward issue cannot resolve , have tried many things make work simply, can save image remote url isolatedstorage , asynchronously.
i have code @ moment can't seem streamline it, post if no suitable alternatives posted if needed.
try class. hope it'll you.
start grabbing use:
imagegrabber grabber = new imagegrabber(); grabber.grabimage(@"http://www.google.com.ua/images/srpr/nav_logo25.png");
btw in example i've used nice method allow read bytes stream (even if stream doesn't support seek operation).
public class imagegrabber { public void grabimage(string url) { httpwebrequest request = (httpwebrequest)webrequest.create(url); request.method = "post"; request.begingetrequeststream(requestcallback, request); } private void requestcallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; request.begingetresponse(getresponsecallback, request); } private void getresponsecallback(iasyncresult asynchronousresult) { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; // end operation httpwebresponse response = (httpwebresponse)request.endgetresponse(asynchronousresult); stream streamresponse = response.getresponsestream(); byte[] bytes = readtoend(streamresponse); //save image isolated storage file.png isolatedstoragefile.getuserstoreforapplication().createfile("file.png").write(bytes, 0, bytes.count()); } private static byte[] readtoend(stream stream) { long originalposition = stream.position; stream.position = 0; try { byte[] readbuffer = new byte[4096]; int totalbytesread = 0; int bytesread; while ((bytesread = stream.read(readbuffer, totalbytesread, readbuffer.length - totalbytesread)) > 0) { totalbytesread += bytesread; if (totalbytesread == readbuffer.length) { int nextbyte = stream.readbyte(); if (nextbyte != -1) { byte[] temp = new byte[readbuffer.length * 2]; buffer.blockcopy(readbuffer, 0, temp, 0, readbuffer.length); buffer.setbyte(temp, totalbytesread, (byte)nextbyte); readbuffer = temp; totalbytesread++; } } } byte[] buffer = readbuffer; if (readbuffer.length != totalbytesread) { buffer = new byte[totalbytesread]; buffer.blockcopy(readbuffer, 0, buffer, 0, totalbytesread); } return buffer; } { stream.position = originalposition; } } }
file saved in isolated storage:
{system drive}:\users\{user name}\appdata\locallow\microsoft\silverlight\is\{bunch of autogenerated folders}\file.png
Comments
Post a Comment