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 回答