【问题标题】:WriteableBitmap and scaling factorWriteableBitmap 和缩放因子
【发布时间】:2023-03-21 05:56:01
【问题描述】:

由于图像闪烁,我使用了 WriteableBitmap。我有具有 3 个缩放因子的图像。但是 GetFileFromApplicationUriAsync 总是读取具有最高缩放因子的图像,并且图像在移动设备上太大。 这是我的代码:

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

【问题讨论】:

  • 为什么没有 3 个不同大小的图片文件,比如contactSmall.png、contactMedium.png 和contactLarge.png?除此之外,尚不清楚为什么从您的方法返回的 WriteableBitmap 会避免 BitmapImage 产生的任何闪烁。
  • 我有contact.scale-100.png、contact.scale-200.png、contact.scale-400.png。如果我直接将 uri 绑定到 Imagesource,系统会知道选择哪个缩放因子,但是当我更改图像时它总是闪烁。 BitmapImage/WriteableBitmap 解决了闪烁,但它总是以最大比例因子拍摄图像。
  • 它会起作用的。但我不想手动执行此操作。系统应该根据当前屏幕分辨率等决定使用哪个图像。如果我这样做:'ImageSource="/Images/contant.png"',它使用图像,这是合适的。
  • 奇怪。根据this page,自动缩放也应该与 GetFileFromApplicationUriAsync 一起使用。
  • 我不知道,为什么它不起作用。应该是的。

标签: xaml windows-runtime uwp winrt-xaml


【解决方案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

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 2017-10-15
    • 2016-05-20
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多