【问题标题】:Open scaled image file in WP 8.1 app from LocalCache从 LocalCache 在 WP 8.1 应用程序中打开缩放的图像文件
【发布时间】:2015-11-02 02:15:00
【问题描述】:

我开发了一个 Windows Phone 8.1 应用程序,它从我们的服务器下载一些数据并将这些文件复制到 ApplicationData.Current.LocalCacheFolder 文件夹中。文件夹中还有不同比例的图片。例如:

  • image.scale-100.png
  • image.scale-140.png
  • image.scale-240.png

问题是如果不传递正确的文件名,我就无法访问这些文件。如果我尝试打开 image.png 它将失败。但是,如果我尝试打开例如image.scale-140.png 效果很好。

如果我将图像作为资源插入到我的项目文件夹中,应用会决定哪个图像具有正确的显示缩放比例并选择它们。

我可以让我的应用自动选择正确的图像吗?还是我必须手动确定缩放比例并确定存在哪些图像?

【问题讨论】:

    标签: c# image windows-phone-8.1 image-scaling


    【解决方案1】:

    你可以使用

    Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
    

    这是所需的缩放比例,因此您可以尝试找到最接近实际所需缩放比例的图像,例如:

    var possibleRatios = new List<double>();
    possibleRatios.Add(1.0);
    possibleRatios.Add(1.4);
    possibleRatios.Add(2.4); //etc.
    double ratio = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
    double closest = possibleRatios.Aggregate((x,y) => Math.Abs(x-ratio) < Math.Abs(y-ratio) ? x : y);
    
    string imageName = "image.scale-" + (int(closest * 100)).ToString() + ".png";
    

    查找最接近缩放的算法取自该问题的答案: How to get the closest number from a List with LINQ?

    【讨论】:

    • 我目前甚至将这个想法加入到应用程序中。但主要问题是,我必须检查哪些文件存在以确定正确的文件名。有什么想法吗?
    • 也许您可以创建一个可以从 120、140 等字符串构造的缩放类。然后您可以遍历文件名,提取缩放字符串并选择最适合的文件.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多