从绝对的Uri获取流

时间:2011-09-22 03:29:39

标签: windows-phone-7 silverlight-4.0

我正在使用Windows Phone 7,我遇到了一个非常棘手的问题。请帮我 ! 我想从png图像的绝对uri(来自web)获得一个流。但GetResourceStream方法仅适用于相对uri。然后我发现了imagetool形式http://imagetools.codeplex.com/,但到现在我的问题仍未解决。 谁能给我一个解决方案?

3 个答案:

答案 0 :(得分:3)

如何使用HttpWebRequestHttpWebResponse

var uri = new Uri("http://chriskoenig.net/wp-content/uploads/2011/04/givecamp_125125_ad.jpg", UriKind.Absolute);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.BeginGetResponse((ar) =>
{
    var response = request.EndGetResponse(ar);
    Dispatcher.BeginInvoke(() =>
    {
        using (var stream = response.GetResponseStream())
        {
            var image = new BitmapImage();
            image.SetSource(stream);
            MyImage.Source = image;
        }
    });
}, null);

答案 1 :(得分:0)

试试这个,

        BitmaiImage bmp=new BitmaiImage();
        Image image=new Image();

        Uri url = new Uri("http://Ur url", UriKind.Absolute);
        HttpWebRequest reqest = (HttpWebRequest)WebRequest.Create(url);
        reqest.BeginGetResponse(DownloadImageCallback, reqest);    

    void DownloadImageCallback(IAsyncResult result)
    {
        HttpWebRequest req = (HttpWebRequest)result.AsyncState;
        HttpWebResponse responce = (HttpWebResponse)req.EndGetResponse(result);
        Stream s = responce.GetResponseStream();
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            bmp.SetSource(s);
            image.Source=bmp;
        });
    }

答案 2 :(得分:0)

从Absolute url尝试这个简单的代码图像流并存储到独立存储

namespace eQuadrigaWP7
{
public class ItemViewModel : INotifyPropertyChanged
{
private string _imgURL;
public string imgURL
{
get
{
return _imgURL;
}
set
{
if (value != _imgURL)
{
_imgURL = value;
}
}
}
private BitmapImage _Image;
public BitmapImage Iimage
{
get
{
return _Image;
}
set
{
if (value != _Image)
{
_Image = value;
}
}
}

public void LoadIimage()
{
if (this.imgURL == null) throw new Exception("Error equadriga log");
HttpWebRequest downloadthumbnailrequest = (HttpWebRequest)WebRequest.Create(new Uri(this._imgURL));   ///this is main
DownloadThumbNailState thumbnailState = new DownloadThumbNailState();
thumbnailState.AsyncRequest = downloadthumbnailrequest;
downloadthumbnailrequest.BeginGetResponse(new AsyncCallback(HandleThumNailDownLoadResponse),
thumbnailState); 
 }

private void HandleThumNailDownLoadResponse(IAsyncResult asyncResult)
{
DownloadThumbNailState thumbnailState = (DownloadThumbNailState)asyncResult.AsyncState;
HttpWebRequest downloadthumbnailrequest = (HttpWebRequest)thumbnailState.AsyncRequest;
thumbnailState.AsyncResponse = (HttpWebResponse)downloadthumbnailrequest.EndGetResponse(asyncResult);
Stream imageStream = thumbnailState.AsyncResponse.GetResponseStream();
byte[] b = new byte[imageStream.Length];
imageStream.Read(b,0,Convert.ToInt32(imageStream.Length));
imageStream.Close();
MemoryStream ms = new MemoryStream(b);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(ms);
String tempJPEG = "logo.jpg";

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
WriteableBitmap wb = new WriteableBitmap(bmp);
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
this.Iimage = bmp;
}
});

}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DownloadThumbNailState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
}
}


ItemViewModel imageitem = new ItemViewModel();
imageitem.imgURL = "http://www.yoursite.in/bilder/9780199738663/titel.jpg";
imageitem.LoadIimage();