2

I develop a Windows Phone 8.1 app that downloads some data from our server and copy these files into the ApplicationData.Current.LocalCacheFolder folder. There are also pictures in the folders with different scaling’s. For example:

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

The problem is that I cannot access this files without passing the correct file name. If I try to open image.png it will fail. But if I try to open e.g. image.scale-140.png it works well.

If I insert the images into my project Folder as a resource the app decides which image has the correct scaling for the Display and choose them.

Can I get my app to choose the right image automatically? Or must I determine the scaling manually and determine which images exists?

Koopakiller
  • 2,838
  • 3
  • 32
  • 47

1 Answers1

0

You could use

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

This is the required scaling, so you could try to find the image that's the closest to the actual required scaling, for example like this:

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";

The algorithm for finding the closest scaling was taken from the answer to this question: How to get the closest number from a List with LINQ?

Community
  • 1
  • 1
mrbraitwistle
  • 414
  • 4
  • 5
  • I impemented even this idea currently into the app. But the main problem is, that I have to check which files exists to determine the correct file name. Any ideas? – Koopakiller Aug 19 '15 at 12:46
  • Maybe you could make a Scaling class that can be constructed from a string like 120, 140, etc. Then you could just iterate through the files your filenames, extract the scaling string and choose the best suited file. – mrbraitwistle Aug 19 '15 at 19:05