none
Get image from async method RRS feed

  • Pregunta

  • Hi, I'm trying to download an image using this code:

    HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("http:/xxxxx.com/image.jpg");
    
    r.Method = "GET";
    r.BeginGetResponse(new AsyncCallback(arg =>
    {
        HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)arg.AsyncState).EndGetResponse(arg);
    
        BitmapImage bmp = new BitmapImage();
        bmp.SetSource(response.GetStream());
    }), r);

    I'd like to code a method that returns the bmp, something like this:

    public BitmapImage DownloadImage(string url)
    {
        HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create(url);
    
        r.Method = "GET";
        r.BeginGetResponse(new AsyncCallback(arg =>
        {
            HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)arg.AsyncState).EndGetResponse(arg);
    
            BitmapImage bmp = new BitmapImage();
            bmp.SetSource(response.GetStream());
        }), r);
        return bmp;
    }


    I know that this method generates an error. I tried to use this:

    IAsyncResult.AsyncWaitHandle.WaitOne();

    but generates a NotsupportedErrorException.

    Any idea to fix that?



    lunes, 14 de enero de 2013 7:59