2016-10-17 118 views
0

由於圖像閃爍我使用了WriteableBitmap。我有3個比例因子的圖像。但是GetFileFromApplicationUriAsync總是讀取具有最高縮放因子的圖像,並且移動設備上的圖像太大。 這裏是我的代碼:WriteableBitmap和縮放因子

private async Task<WriteableBitmap> CreateBitmapImage(Uri uri) 
    { 
     var file = await StorageFile.GetFileFromApplicationUriAsync(uri); 
     BitmapImage bitmapImage; 
     WriteableBitmap writeableBitmap; 
     using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) 
     { 
      bitmapImage = new BitmapImage(); 
      await bitmapImage.SetSourceAsync(fileStream); 
      writeableBitmap = new WriteableBitmap(bitmapImage.PixelHeight, bitmapImage.PixelWidth); 
      fileStream.Seek(0); 
      await writeableBitmap.SetSourceAsync(fileStream); 
     } 
     return writeableBitmap; 
    } 

我使用的BitmapImage因爲WriteableBitmap的需求像素高度和寬度在構造函數的輸入參數。 URI是如:MS-APPX:///Images/contact.png

+0

爲什麼沒有3個不同大小的圖像文件,如contactSmall.png,contactMedium.png和contactLarge.png?除此之外,目前還不清楚爲什麼從您的方法返回的WriteableBitmap會避免BitmapImage產生閃爍。 – Clemens

+0

我有contact.scale-100.png,contact.scale-200.png,contact.scale-400.png。如果我直接將uri綁定到Imagesource,那麼系統知道,選擇哪個縮放因子,但是當我更改圖像時,它會始終閃爍。 BitmapImage/WriteableBitmap解決了閃爍問題,但它總是以最高的比例因子拍攝圖像。 – JuP

+0

它會工作。但我不想手動執行此操作。系統應該根據當前的場景分辨率等來決定使用哪個圖像。如果我這樣做:'ImageSource ='/ Images/contant.png'',它使用適合的圖像。 – JuP

回答

1

我已經解決了這種方式:

<Image Grid.Column="0" x:Name="StatusImage" Margin="0,8,12,8" Source="{x:Bind ImageStatusUri, Mode=OneWay}"> 
        <interactivity:Interaction.Behaviors> 
         <core:EventTriggerBehavior EventName="ImageOpened"> 
          <core:ChangePropertyAction PropertyName="Source" Value="{Binding ElementName=StatusImage, Path=Source}" TargetObject="{Binding ElementName=CopyOfStatusImage}"/> 
         </core:EventTriggerBehavior> 
        </interactivity:Interaction.Behaviors> 
       </Image> 
<Image Grid.Column="0" Margin="0,8,12,8" x:Name="CopyOfStatusImage"/> 

所以我並不需要使用WriteableBitmap的和GetFileFromApplicationUriAsync