2017-03-06 8 views
1

변환기를 사용하여 XAML에로드 된 이미지가 있습니다. 이 이미지를 다시로드하는 대신 이미지를 가져 와서 페이지의 다른 그래픽에 사용할 수있는 주 색상을 찾고 싶습니다. 지금까지이있다 :UWP BitmapImage to Stream

var himage = (BitmapImage)image_home.Source; 

using (var stream = await himage.OpenReadAsync()) //**can't open himage this way** 
    { 
     //Create a decoder for the image 
     var decoder = await BitmapDecoder.CreateAsync(stream); 

     //Create a transform to get a 1x1 image 
     var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 }; 

     //Get the pixel provider 
     var pixels = await decoder.GetPixelDataAsync(
     BitmapPixelFormat.Rgba8, 
     BitmapAlphaMode.Ignore, 
     myTransform, 
     ExifOrientationMode.IgnoreExifOrientation, 
     ColorManagementMode.DoNotColorManage); 

     //Get the bytes of the 1x1 scaled image 
     var bytes = pixels.DetachPixelData(); 

     //read the color 
     var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]); 
    } 
분명히

내가 OpenReadAsync 사용하여 BitmapImage의 himage를 열 수 없습니다, 어떻게이를 달성 할 수 있도록 내가 거기에 어떻게해야합니까?

+0

언급 한 변환기로 무엇을하고 있습니까? 같은 시간에 지배적 인 색을 추출 할 수 없습니까? –

+0

죄송합니다. ID 번호를 이미지 소스 URL로 변환하는 바인드 변환기를 명시해야합니다. –

답변

0

BitmapDecoder은 새 인스턴스를 만드는 데 RandomAccessStream 개체가 필요합니다. BitmapImage은 원본 소스를 모른다면 RandomAccessStream으로 직접 추출하지 못할 수도 있습니다. 귀하의 의견에 따르면, 당신은 이미지 컨트롤에 이미지 Uri를 바인딩하고 있으므로 원래 소스를 알 수 있고 을 RandomAccessStreamReference 클래스로 BitmapImageUriSource 속성에서 가져올 수 있으므로 이미지를 다시로드 할 필요가 없습니다. 코드는 다음과 같습니다.

var himage = (BitmapImage)image_home.Source; 
RandomAccessStreamReference random = RandomAccessStreamReference.CreateFromUri(himage.UriSour‌​ce); 

using (IRandomAccessStream stream = await random.OpenReadAsync()) 
{ 
    //Create a decoder for the image 
    var decoder = await BitmapDecoder.CreateAsync(stream); 
    ... 
    //read the color 
    var myDominantColor = Color.FromArgb(255, bytes[0], bytes[1], bytes[2]); 
} 
+0

감사합니다. @ sunteen-wu-msft이 때마다 myDominantColour가 실행될 때마다 # FF000000을 반환하므로 문제가있는 것 같습니다. 무엇이 이미지인가? –