Question

I am trying to run my app for different resolution windows phones. I have taken help from here

This is my Code

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Net;
     using System.Windows;
     using System.Windows.Controls;
     using System.Windows.Navigation;
     using Microsoft.Phone.Controls;
     using Microsoft.Phone.Shell;
     using System.Windows.Media.Imaging;
     using reso.Resources;

     namespace reso
     {
         public partial class MainPage : PhoneApplicationPage
         {
    // Constructor
    public MainPage()
    {
        InitializeComponent();


        Image myImage = new Image();

        if (MultiRes.Is720p)
            myImage.Source = new BitmapImage(new Uri("overlay_480x853.png"));
        else if (MultiRes.IsWvga)
            myImage.Source = new BitmapImage(new Uri("background.png"));
        else if (MultiRes.IsWxga)
            myImage.Source = new BitmapImage(new Uri("background.png"));


    }



    private void MutliRes_ScreenSize(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(
            "ActualHeight: " + Application.Current.Host.Content.ActualHeight + Environment.NewLine +
            "ActualWidth: " + Application.Current.Host.Content.ActualWidth + Environment.NewLine +
            "ScaleFactor : " + Application.Current.Host.Content.ScaleFactor + Environment.NewLine);
    }



    private void MultiRes_Resolution(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(
            "IsHD: " + MultiRes.IsHighResolution + Environment.NewLine +
            "Resolution: " + MultiRes.CurrentResolution);
    }

    public static class MultiRes
    {
        public static bool IsHighResolution
        {
            get { return Application.Current.Host.Content.ScaleFactor > 100; }
        }

        public static bool IsLowResolution
        {
            get { return Application.Current.Host.Content.ScaleFactor <= 100; }
        }

        public static bool IsWvga
        {
            get
            {
                return Application.Current.Host.Content.ActualHeight == 800
                    && Application.Current.Host.Content.ScaleFactor == 100;
            }
        }

        public static bool IsWxga
        {
            get { return Application.Current.Host.Content.ScaleFactor == 160; }
        }

        public static bool Is720p
        {
            get { return Application.Current.Host.Content.ScaleFactor == 150; }
        }

        public static String CurrentResolution
        {
            get
            {
                if (IsWvga) return "WVGA";
                else if (IsWxga) return "WXGA";
                else if (Is720p) return "HD720p";
                else throw new InvalidOperationException("Unknown resolution");
            }
        }
    }




}

}

but it is giving error 'System.UriFormatException' in

          myImage.Source = new BitmapImage(new Uri("background.png"));   

I am not able to get what exatly is wrong, i am new to windows app development so please help me out to solve this problem.
Thanks in advance for any help.

Was it helpful?

Solution

You need to add 2 parameters, while you initializes a new instance of the Uri class .

new Uri("background.png", UriKind.Relative)

Next step is to add image to the project.

enter image description here

Image Path:

Assets/background.png

The last step is to add image to the View.

LayoutRoot.Children.Add(myImage);

        Image myImage = new Image();

        myImage.Source = new BitmapImage(new Uri("Assets/background.png", UriKind.Relative));

        LayoutRoot.Children.Add(myImage);

OTHER TIPS

Always check that the Build Action Property in the Image is set to Content

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top