locked
get image source URI

    Question

  • hi

    i have this code :

    if (image.Source==new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/13.png")))
                {
                    txtTop.Text = "Red coca";
                }

    but not worked for me.

    how can i get image.source URI ?

    i need to get source image.

    tank you


    Amir Ameri

    Tuesday, February 10, 2015 2:16 PM

Answers

  • assignint the source can be written much simpler:
    Random Rl = new Random();
                int value = Rl.Next(1, 17);
    
                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(string.Format("ms-appx:Assets/Food/{0:00}.png", value)));
                image11.Tag = value;

    and in the click handler you can do this:
    switch((int)image11.Tag)
                {
                    case 1:
                        break;
                    case 2:
                        break;
                }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Thursday, February 12, 2015 7:30 PM
    Tuesday, February 10, 2015 4:36 PM
  • {0:00} the first 0 (the one before :) is the parameter index; behind the : is format. makes sure numbers always formatted as 2 decimal so 4 becomes 04 and 11 stays 11

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Thursday, February 12, 2015 7:30 PM
    Thursday, February 12, 2015 11:18 AM

All replies

  • Hello AMIR ameri,

    the URI should be:
    new Uri("ms-appx:///Assets/Food/13.png")

    Regards,
    Bo Liu
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    Tuesday, February 10, 2015 2:58 PM
  • i know it.

    but i want get source from image control.

    i want worked this function :

    if (image.Source==new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/13.png")))
                {
                    txtTop.Text = "Red coca";
                }

    if image source == for example 13.png then do some thing

    if image source == for example 14.png then do some thing

    ....

    this is my problem.


    Amir Ameri

    Tuesday, February 10, 2015 3:08 PM
  • Hello AMIR ameri,

    you cannot directly compare two BitmapImage with "==".
    Read the thread at [1]. This thread introduced a method for BitmapImage compare.

    [1] http://stackoverflow.com/questions/15558107/quickest-way-to-compare-two-bitmapimages-to-check-if-they-are-different-in-wpf

    Regards,
    Bo Liu
    Developer-Hotline for MSDN Online Germany

    Disclaimer:
    Please take into consideration, that further inquiries cannot or will be answered with delay.
    For further information please contact us per telephone through the MSDN-Entwickler-Hotline: http://www.msdn-online.de/Hotline
    MSDN-Entwickler-Hotline: Fast and professional help for software developers free of charge!

    For this post by the MSDN-Entwickler-Hotline the following terms and conditions apply: Trademarks, Privacy as well as the separate terms of use for the MSDN-Entwickler-Hotline .

    Tuesday, February 10, 2015 3:40 PM
  • maybe you can use the tag property for this scenario. in xaml:
     <Image Tag="myIMage" x:Name="MyImage" />
    and in code
    if (image.Tag == "MyImage")



    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 10, 2015 3:47 PM
  • i change  image source in my app random( i have 20 picture) .

    then i want  If image01.source for example equal to image02.source do some thing. 


    Amir Ameri

    Tuesday, February 10, 2015 4:04 PM
  • when you change the source change the tag? doing a switch on the iamge going be hard.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 10, 2015 4:12 PM
  •  Random Rl = new Random();
     Random Rr = new Random();

                        switch (Rl.Next(1, 17))
                        {
                            case (1):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/01.png"));
                                break;
                            case (2):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/02.png"));
                                break;
                            case (3):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/03.png"));
                                break;
                            case (4):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/04.png"));
                                break;
                            case (5):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/05.png"));
                                break;
                            case (6):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/11.png"));
                                break;
                            case (7):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/12.png"));
                                break;
                            case (8):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/13.png"));
                                break;
                            case (9):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/14.png"));
                                break;
                            case (10):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/15.png"));
                                break;
                            case (11):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/16.png"));
                                break;
                            case (12):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/17.png"));
                                break;
                            case (13):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/18.png"));
                                break;
                            case (14):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/19.png"));
                                break;
                            case (15):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/20.png"));
                                break;
                            case (16):
                                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/21.png"));
                                break;
                        }


                        for (int i = 0; i < 20; i++)
                        {



                            switch (Rr.Next(1, 17))
                            {
                                case (1):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/21.png"));
                                    break;
                                case (2):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/20.png"));
                                    break;
                                case (3):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/19.png"));
                                    break;
                                case (4):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/18.png"));
                                    break;
                                case (5):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/17.png"));
                                    break;
                                case (6):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/16.png"));
                                    break;
                                case (7):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/15.png"));
                                    break;
                                case (8):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/14.png"));
                                    break;
                                case (9):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/13.png"));
                                    break;
                                case (10):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/12.png"));
                                    break;
                                case (11):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/11.png"));
                                    break;
                                case (12):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/01.png"));
                                    break;
                                case (13):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/02.png"));
                                    break;
                                case (14):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/03.png"));
                                    break;
                                case (15):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/04.png"));
                                    break;
                                case (16):
                                    image12.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:Assets/Food/05.png"));
                                    break;

                            } 

    Amir Ameri

    Tuesday, February 10, 2015 4:28 PM
  • assignint the source can be written much simpler:
    Random Rl = new Random();
                int value = Rl.Next(1, 17);
    
                image11.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri(string.Format("ms-appx:Assets/Food/{0:00}.png", value)));
                image11.Tag = value;

    and in the click handler you can do this:
    switch((int)image11.Tag)
                {
                    case 1:
                        break;
                    case 2:
                        break;
                }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Thursday, February 12, 2015 7:30 PM
    Tuesday, February 10, 2015 4:36 PM
  • tanx dave

    but i cant understand  what is {0:00}.png

    in fact  what happend for  image11.source 

    for example if value=4 then image11.source=??

    is this 4.png?

    and finally i want comparison between 2 image.source that if image.sorce=banana.png then do some thing 


    Amir Ameri

    Thursday, February 12, 2015 11:01 AM
  • {0:00} the first 0 (the one before :) is the parameter index; behind the : is format. makes sure numbers always formatted as 2 decimal so 4 becomes 04 and 11 stays 11

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by AMIR ameri Thursday, February 12, 2015 7:30 PM
    Thursday, February 12, 2015 11:18 AM
  • tank you very much Mr Dave Smits

    You've helped me a lot.


    Amir Ameri

    Thursday, February 12, 2015 7:30 PM