0

携帯電話に画像をダウンロードするアプリがあり、画像のカテゴリに応じてニュースフィードに割り当てられます。私はこの関数を使用しています:

    private static void DownloadImage(string furl, string ids)
    {
        // Connect Again to the API
        WebClient client = new WebClient();
        client.Headers["NewsID"] = ids;

        string url = "www.xxx.com/image/xyz";

        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        client.OpenReadAsync(new Uri(url));
    }

    private static void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (e.Error == null && !e.Cancelled)
            {
                Stream reply = null;
                StreamReader s = null;

                // i am not able to read the sender who is a webclient to retrieve the information it is always skipping it 
                WebClient wcd = sender as WebClient;

                reply = (Stream)e.Result;
                s = new StreamReader(reply);
                //Console.WriteLine(s.ReadToEnd());
                s.Close();
                reply.Close();

                if (!myIsolatedStorage.DirectoryExists("ImageCache"))
                {
                    myIsolatedStorage.CreateDirectory("ImageCache");
                }

                //try
                //{//((MS.Internal.InternalMemoryStream)(e.Result)).FinalUri.Segments[2]
                var graphImage = e.Result;
                Random rand = new Random();
                string fileName = string.Format("ImageCache/{0}.jpg", rand.Next());
                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);

                BitmapImage image = new BitmapImage();
                image.SetSource(e.Result);
                WriteableBitmap wb = new WriteableBitmap(image);

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

                //}
                //catch (IsolatedStorageException ex)
                //{
                //IsolatedStorageException
                //Exception handle appropriately for your app  
                //}
            }
        }
    }

画像のダウンロードに使用している関数で、ヘッダーからを取得し、データベースに保存する前に画像に割り当てOpenReadCompleteたいと思います。newsIDヘッダーにアクセスできないようです。これは可能ですか?

4

1 に答える 1

0

差出人と一緒に読めると思います。

WebClient c = (WebClient)sender;
        string id = c.Headers["NewsID"];
于 2013-01-25T14:45:59.950 に答える