2013-01-11 2 views
0

내 앱에 특정 이미지가 사전로드되어야하는 구성 요소가 있습니다. 내 생각에 웹에서 비트 맵 객체로 모든 이미지를 가져온 다음 내 이미지에서 소스로 사용해야합니다. 가장 쉬운 방법은 무엇입니까? 이미 이미지를 다운로드하는 코드의 비트를 작성했습니다,하지만 여러 이미지와 함께 작동하지 않습니다 ...비트 맵 배열로 이미지 목록 다운로드 C#

HttpClient client = new HttpClient(); 
     NetResult result; 

     try 
     { 

      Debug.WriteLine("Starting call to " + Url); 
      HttpResponseMessage response = await client.GetAsync(Url); 
      response.EnsureSuccessStatusCode(); 
      result = await this.processContent(response.Content); 

     } 
     catch (Exception e) 
     { 

      Debug.WriteLine(e.Message); 
      Debug.WriteLine("Unable to load " + Url); 
      result = new NetResult(NetResultStatus.Failure, "Could not connect to " + Url + "."); 

} 

및 ProcessContent는 다음과 같이 작동합니다

public override async Task<NetResult> processContent(HttpContent content) 
    { 

     InMemoryRandomAccessStream randomAccessStream = null; 

     if (content != null && !Constants.FakeInternet) 
     { 
      byte[] img = await content.ReadAsByteArrayAsync(); 
      // Transform to a stream 
      randomAccessStream = new InMemoryRandomAccessStream(); 
      DataWriter writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0)); 
      writer.WriteBytes(img); 
      await writer.StoreAsync(); 
     } 

     _m = new ManualResetEvent(false); 

     (Application.Current as App).UIDispatcher.DispatchAsync(() => 
      { 


        // Create bitmap image 
        image = new BitmapImage(); 

        NetImage i = (NetImage)this; 

        Debug.WriteLine("Creating Image " + Url); 
        image.ImageFailed += ImageFailed; 
        image.ImageOpened += ImageOpened; 
        image.SetSource(randomAccessStream); 


      }); 

     _m.WaitOne(); 


     return await base.processContent(content); 

    } 

    void ImageOpened(object sender, Windows.UI.Xaml.RoutedEventArgs e) 
    { 

     Debug.WriteLine("Image opened: " + Url); 
     _m.Set(); 

    } 

    void ImageFailed(object sender, Windows.UI.Xaml.ExceptionRoutedEventArgs e) 
    { 

     Debug.WriteLine("Image failed: " + Url); 
     image = null; 
     ConnectionState = ConnectionState.Failed; 
     _m.Set(); 

    } 

그물에서 비트 맵 객체로 PNG를 다운로드 할 때 사용할 수있는 내장 API가 있습니까? 짜증나는 일 중 하나는 UI 스레드에서 비트 맵을 디코딩해야한다는 것입니다 ... 왜 그런가요?

답변

0

PNG를 다운로드하고 BitmapImage를 만드는 몇 가지 단계를 저장하는 WinRT API에 대해 알지 못합니다. 그렇다고해서 존재하지 않는다는 의미는 아닙니다.

XAML Images Sample의 시나리오 4를 보면 도움이 될 수 있습니다. 비동기 적으로 배경 스레드에서 프랙탈을 계산 한 다음 결과를 WriteableBitmap에 표시합니다. 이렇게하면 UI 스레드로부터 벗어날 수 있습니다.

+0

감사합니다. 적어도 도움이 되네요. UI 스레드에 있어야하지 않아도 디스패처와 manualresetevent를 사용하면 여러 이미지를 동시에 하나 이상의 이미지를 다운로드하려고 할 때 혼란스럽게 할 수 있습니다. . –