How to Serialise Images from Remote URL to IsolateStorage or to XML?
I need to download images from a rem开发者_如何学Goote URL and serialise them to Isolated Storage, but am having trouble with figuring out how to get this to work, I'm currently serialising the URIs to the Images - and can get this to work, but want to download the image and store it in the file system, what is the best way to do this using C# and Silverlight.
I've tried finding some ways to do this, but they are too complicated, if possible I can store Image Data in the XML if this is a solution, but I just want to download the file - .NET on the desktop has many methods to do this sort of thing, but I need a Silverlight Solution.
If there are any examples that demonstrate this sort of thing, then this may help too - it seems like a straightforward issue but I cannot resolve it and have tried many things to make this work simply, so that I can save an Image from a remote URL to IsolatedStorage and do this Asynchronously.
I do have some code that does this at the moment but I can't seem to streamline it, will post it if no suitable alternatives are posted if needed.
Try this class. I hope it'll help you.
To start grabbing use:
ImageGrabber grabber = new ImageGrabber();
grabber.GrabImage(@"http://www.google.com.ua/images/srpr/nav_logo25.png");
BTW in this example I've used very nice method which allow to read bytes from stream (even if this 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 the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
byte[] bytes = ReadToEnd(streamResponse);
//save image to isolated storage as 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;
}
finally
{
stream.Position = originalPosition;
}
}
}
File will be saved in isolated storage:
{System drive}:\Users\{User Name}\AppData\LocalLow\Microsoft\Silverlight\is\{bunch of autogenerated folders}\file.png
I've used the following:
WebClient client = new WebClient();
byte[] bytes = client.DownloadData("http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png");
精彩评论