'다운'에 해당되는 글 1건

  1. 2011.03.15 [wp7] 웹에서 이미지를 다운받고 앱에 보여주기
Windows Phone 72011. 3. 15. 17:44

이번에 할려는 일은 About 페이지에 앱 리스트를 쭉 등록시키는일이다.

 

근데 서버로부터 앱리스트를 받고 그걸 뿌려줘야 하는데 xml을 이용하기로 했다.

 

앱은 이렇게 이미지가 나와야 한다.

 

 

description등 과 같은 string이야 xml에서 받아서 파싱하면 땡이지만

 

이미지는? 그래서 테스트 어플을 하나 만들었다.

 

일단 버튼을 하나 만들고

 

Webclient로부터 이미지를 하나 다운 받게 하였다.

 

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var url = "http://4.bp.blogspot.com/_HfTuDK39Rv4/SAjoBeqcFtI/AAAAAAAAAbo/UaXaUjD6yTY/s400/cuty.jpg";
            //var url = "http://fc07.deviantart.net/fs27/i/2008/139/9/9/Cuty_Rabbit_by_Willow_San.jpg";
        
            WebClient webClient = new WebClient();
            webClient.OpenReadAsync(new Uri(url));
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
        }

 

다운로드가 완료되면

 

 

void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var resInfo = new StreamResourceInfo(e.Result, null);
            var reader = new StreamReader(resInfo.Stream);
 
            byte[] contents;
            using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
            {
                contents = bReader.ReadBytes((int)reader.BaseStream.Length);
            }            
            
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists("file.jpg") == true)
                {
                    isf.DeleteFile("file.jpg");
                }
                IsolatedStorageFileStream  stream = new IsolatedStorageFileStream("file.jpg", FileMode.OpenOrCreate, isf);                
                stream.Write(contents, 0, contents.Length);                
                stream.Close();
                MessageBox.Show("Finish Download");
            }
        }

 

다운로드가 완료되면 file.jpg를 하나 만들어서 IsolatedStorageFile에 저장을 한다. 그전에 저렇게 StreamResouceInfo와 StreamReader로 파일을 읽는다.

 

private void Button_Click_2(object sender, RoutedEventArgs e)
       {
           using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
           {
               IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.jpg", FileMode.Open, isf);
               var image = new BitmapImage();
               image.SetSource(stream);
 
               stream.Close();
               xImage.Source = image;
           }
       }

 

자 이제 아까 저장했던 파일을 읽어서 Image Control에 출력해보자…


참고  : http://dotnet.dzone.com/articles/operating-image-files-windows?mz=27249-windowsphone7 

대충 소스 보면 알겠지만 혹시나 모르니 소스를 첨부 한다.



Posted by 동동(이재동)