locked
Getting Current Background Image of a Control? RRS feed

  • Question

  • I need to occasionally determine the current source image of a control, particularly a HyperlinkButton's background. How do I do this? All the PNG image sources are located in the Assets folder, and I can programmatically change the background image, but I still need to understand how I can ask the control what it's current image source is.

    Any pointers appreciated, especially in VB.



    Sunday, December 8, 2013 1:48 AM

Answers

  • Hi David,

    Well, ToString will convert a object to a string by its name, so in your case, you can use the code as below:

    Dim src As ImageBrush = TryCast(HyperlinkButton1.Background, ImageBrush)
    Dim bitmap As BitmapImage = TryCast(src.ImageSource, BitmapImage)
    Dim uri As String = bitmap.UriSource.ToString()
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by David VB2005 Sunday, December 8, 2013 2:05 PM
    Sunday, December 8, 2013 6:39 AM
    Moderator

All replies

  • Hi David,

    Have you tried HyperlinkButton.Background? If a image has been set as the backgound, a ImageBrush item will be returned, and you could find ImageSource from it.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Sunday, December 8, 2013 3:20 AM
    Moderator
  • Thanks, James. How would I inquire on the ImageBrush, though?

            Dim src As ImageBrush
            src = HyperlinkButton1.Background
            txt1.Text = src.ToString

    This code just returns  "Windows.UI.Xaml.Media.ImageBrush" when I need to see, say, "/Assets/image1.png".


    Sunday, December 8, 2013 3:25 AM
  • Yes, you are almost done, not use ToString but use ImageSource.

    Your code could be something like 

           Dim src As ImageBrush
           src = HyperlinkButton1.Background
           txt1.Text = src.ImageSource

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Sunday, December 8, 2013 4:15 AM
    Moderator
  • Hmm. I had to add the ".ToString" at the end, as using just "txt1.Text - src.ImageSource" presents an error saying:

    Value of type 'Windows.UI.Xaml.Media.ImageSource' cannot be converted to 'String'

    When I add the ".ToString" at the end, there's no more error (txt1.Text = src.ImageSource.ToString)

    But the result says only: "Windows.UI.Xaml.Media.Imaging.BitmapImage"

    Sunday, December 8, 2013 5:45 AM
  • Hi David,

    Well, ToString will convert a object to a string by its name, so in your case, you can use the code as below:

    Dim src As ImageBrush = TryCast(HyperlinkButton1.Background, ImageBrush)
    Dim bitmap As BitmapImage = TryCast(src.ImageSource, BitmapImage)
    Dim uri As String = bitmap.UriSource.ToString()
    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by David VB2005 Sunday, December 8, 2013 2:05 PM
    Sunday, December 8, 2013 6:39 AM
    Moderator
  • Thank you, James!
    Sunday, December 8, 2013 2:05 PM