-1

Following up from my question at this link and trying to implement the accepted answer:

Invoking a method asynchronously and/or on its own thread to increase performance

I'm trying to turn a simple method called DownloadImageFromUrl that takes a string Url and returns a Bitmap into one that will run using async. The current method:

private Bitmap DownloadImageFromUrl(string url)
    {
        //// METHOD A:
        //WebRequest request = System.Net.WebRequest.Create(url);
        //WebResponse response = request.GetResponse();
        //Stream responseStream = response.GetResponseStream();
        //return new Bitmap(responseStream);

        // METHOD B:
        using (WebClient client = new WebClient())
        {
            byte[] data = client.DownloadData(url);
            using (MemoryStream mem = (data == null) ? null : new MemoryStream(data))
            {
                return (data == null || mem == null) ? null : (Bitmap)Image.FromStream(mem);
            }
        }
    }

The idea of making this Async is so that in another method, I can use this one to do something like this:

public async Task<HttpResponseMessage> process(string image)
{
 var task = DownloadFromBlobAsync(image);
 var setupData = DoSomeSetup();
 var image = await task;
 return DrawTextOnImage(image, setupData);
}

The DoSomeSetup takes fairly long and so does downloading the image, so i'd like to download the image on its own thread while the setup happens.

I'm not sure what tools are available to change this downloadImageFromUrl to return a task.. Any resources or code examples would be helpful.

Community
  • 1
  • 1
JakeD
  • 407
  • 2
  • 7
  • 19

1 Answers1

0

Sample by charlie from How to download image from url using c#:

    using (WebClient client = new WebClient()) 
  {
    client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
    client.DownloadFile(new Uri(url), @"c:\temp\image35.png");
   }

EDITED

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("fileNameHere");

    BitmapImage bitmap = new BitmapImage();
    var stream = await DownloadFile(new Uri("http://someuri.com", UriKind.Absolute));
    bitmap.SetSource(stream);
    WriteableBitmap wb = new WriteableBitmap(bitmap);

    // Encode WriteableBitmap object to a JPEG stream.
    Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    fileStream.Close();
}

possible duplicated:

How to download image from url using c#

Community
  • 1
  • 1
Vinícius Todesco
  • 1,907
  • 13
  • 14
  • This is the right idea for sure but I want to save the image to a Bitmap probably using memorystream and not to a file – JakeD Jan 10 '17 at 18:18
  • 1
    Please just comment/vote as duplicate instead of copy-pasting someone else answers. Also copy-pasted code/text without attribution violates SO policy (http://stackoverflow.com/help/referencing, http://meta.stackoverflow.com/questions/302295/should-i-fix-plagiarism-that-is-caused-by-missing-attribution). Note that after adding proper attribution it is clear that you did not provide any information of your own. – Alexei Levenkov Jan 10 '17 at 18:20