2

I need to download images from a remote 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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RoguePlanetoid
  • 4,516
  • 7
  • 47
  • 64

2 Answers2

2

I've used the following:

WebClient client = new WebClient();
byte[] bytes = client.DownloadData("http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png");
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Denis Stepanenko
  • 510
  • 6
  • 14
2

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
Community
  • 1
  • 1
Igor V Savchenko
  • 1,076
  • 1
  • 17
  • 32
  • Looks interesting, I was trying to do something like this but couldn't figure it out - looks like this might be helpful, will try this and see how it works with my application – RoguePlanetoid Nov 16 '10 at 10:09
  • This is a good fit to replace my existing method as it wasn't as streamlined as I would have liked - this is a better solution. – RoguePlanetoid Nov 23 '10 at 11:09