Windows8 App2013. 12. 4. 13:29
private async void GetFile()
       {
           Uri source = new Uri(imgUrl);
           StorageFile destinationFile;
           try
           {
               destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                   "downloadimage.jpg", CreationCollisionOption.GenerateUniqueName);
           }
           catch (FileNotFoundException ex)
           {
               return;
           }
           BackgroundDownloader downloader = new BackgroundDownloader();
           DownloadOperation download = downloader.CreateDownload(source, destinationFile);
           await download.StartAsync();
 
           //ResponseInformation response = download.GetResponseInformation();
           var stream = await download.ResultFile.OpenReadAsync();
 
           BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
           InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
           BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);
 
           BitmapBounds bounds = new BitmapBounds();
           bounds.Height = 274;
           bounds.Width = 382;
           bounds.Y = 0;
           bounds.X = 0;
 
           enc.BitmapTransform.Bounds = bounds;
           await enc.FlushAsync();
 
           BitmapImage bImg = new BitmapImage();
           bImg.SetSource(ras);
           xImage.Source = bImg;
 
           //2
           InMemoryRandomAccessStream ras2 = new InMemoryRandomAccessStream();
           BitmapEncoder enc2 = await BitmapEncoder.CreateForTranscodingAsync(ras2, decoder);
           BitmapBounds bounds2 = new BitmapBounds();
           bounds2.Height = 274;
           bounds2.Width = 382;
           bounds2.Y = 274;
           bounds2.X = 0;
 
           enc2.BitmapTransform.Bounds = bounds2;
           await enc2.FlushAsync();
 
           BitmapImage bImg2 = new BitmapImage();
           bImg2.SetSource(ras2);
           xImage2.Source = bImg2;
 
           var filed = await ApplicationData.Current.LocalFolder.GetFilesAsync();
 
           foreach (var item in filed)
           {
               //그냥 모두 삭제 삭제 안된 파일이 있을경우 파일이 커지는경우를 방지
               await item.DeleteAsync();
           }
       }

 

참고

다운로드

http://stackoverflow.com/questions/19272878/how-to-save-image-downloaded-from-urlserver-to-local-folder-in-windows-store-a

 

crop

http://stackoverflow.com/questions/12349611/how-to-resize-image-in-c-sharp-winrt-winmd

 

삭제:

http://stackoverflow.com/questions/14978526/how-delete-file-in-localstorage-on-winrt

Posted by 동동(이재동)
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 동동(이재동)