한 웹 사이트에서 비동기식으로 이미지를 다운로드하고 있습니다. 그리고 이미지 목록을 IsolatedStorage에 저장하려고합니다. 그리고 스트림은 직렬화 할 수 없으므로 바이트 배열로 변환해야합니다. 하지만 ReadFully() 메소드에서는 while 루프에서 Stream을 읽지 않습니다. 여기Windows Phone에서 비동기식으로 다운로드 한 후 스트림을 바이트 배열로 변환
내가 이미지를 다운로드하려고하는 방법이다 :
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Headers["Referer"] = "http://www.website.com";
request.BeginGetResponse((result) =>
{
Stream imageStream = request.EndGetResponse(result).GetResponseStream();
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Set stream as the source of image
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
bitmapImage.SetSource(imageStream);
image.Source = bitmapImage;
// Convert stream to byte array and save in the custom list with the uri of the image
ls.Add(new DownloadedImages() { Image = ReadFully(imageStream), URI = uri });
ds.SaveMyData(ls, "BSCImages");
});
}, null);
을 그리고 여기 바이트 배열 스트림을 변환하는 방법입니다 :
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[input.Length];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
업데이트 :
그것은이다 while 루프 안에 들어 가지 않습니다. 따라서 바이트 배열은 항상 비어 있습니다.
문제는 무엇인가에 전달할 다음의
byte[]
거야? 스트림을 읽지 않는다는 것은 무엇을 의미합니까? 어떤 예외가 있습니까? 스트림 길이는 0입니까? –@LeoLorenzoLuis 질문을 업데이트했습니다 –