2011-09-22 125 views
0

我正在使用Windows Phone 7,我有一個非常困難的問題。請幫幫我 ! 我想從一個PNG圖像的絕對uri(從網頁)流形式。但是GetResourceStream方法僅適用於相對uri。然後我發現imagetool形式http://imagetools.codeplex.com/,但到現在我的問題還沒有解決。 任何人都可以給我一個解決方案嗎?從絕對Uri獲取流

+0

什麼是你真正想達到什麼目的?你爲什麼需要圖像?你想立即顯示它嗎?存儲它?給我們更多的背景,以便我們能夠更好地幫助你。 – invalidusername

回答

0

嘗試從絕對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(); 
2

如何使用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); 
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; 
     }); 
    }