2016-10-17 115 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