locked
How do I find out what the current image is in a Image Control? VB 2013 Store App

    Question

  • How do I find out what the current image in a Image Control is? This will be happening in a click event, so I will be getting the info from sender.

    Thanks,

    David

    Friday, June 06, 2014 3:01 AM

Answers

  • There is no universal way to do this. You can get the Image's Source, but that may not have come from a named source. It could have been streamed in or be in a WriteableBitmap.

    You can try getting the Image.Source property, casting that to a BitmapImage, and then seeing if it's UriSource property is set.

    BitmapImage bm = myImage.Source as BitmapImage;
    if (bm != null)
    {
       // This may or may not be set
       Uri imageOrigin = bm.UriSource;
       Debug.WriteLine("Origin: {0}", imageOrigin );
    }

    • Marked as answer by David.Warwick Saturday, June 07, 2014 1:36 AM
    Friday, June 06, 2014 5:38 AM
    Owner

All replies

  • There is no universal way to do this. You can get the Image's Source, but that may not have come from a named source. It could have been streamed in or be in a WriteableBitmap.

    You can try getting the Image.Source property, casting that to a BitmapImage, and then seeing if it's UriSource property is set.

    BitmapImage bm = myImage.Source as BitmapImage;
    if (bm != null)
    {
       // This may or may not be set
       Uri imageOrigin = bm.UriSource;
       Debug.WriteLine("Origin: {0}", imageOrigin );
    }

    • Marked as answer by David.Warwick Saturday, June 07, 2014 1:36 AM
    Friday, June 06, 2014 5:38 AM
    Owner
  • Hi Rob. Thanks for the reply. I will give that a try and let you know know how it works. If not, I guess I could always just create a variable to keep track of what image is in each ImageBox.

    David

    Friday, June 06, 2014 12:45 PM
  • I needed the same thing once this is how I 've done it:

    var myImage = (FrameworkElement)sender as Image;
    string ImageName= ((BitmapImage)myImage.Source).UriSource.ToString();

    If you have allot of images and you want want to check with a singe Click_Event this does the job.

    Friday, June 06, 2014 8:56 PM
  • St4mos, your code is not general purpose. It will work only if you control the inputs and can guarantee that the image's sources were all BitmapImages loaded from a Uri. Otherwise it will crash with a NullReferenceException.

    My code snippet was essentially the same, but with the check to prevent the crash.

    --Rob

    Friday, June 06, 2014 9:09 PM
    Owner
  • Rob, I am not a C guy. How would I write this in VB for Windows Store?
    Saturday, June 07, 2014 1:22 AM
  • st4mos, can you show me that in VB? I am not a C guy.

    Thanks,

    David

    Saturday, June 07, 2014 1:23 AM
  • Never mind. I figured out the VB translation, and you solution solves the problem. Now I have to figure out Left and Right String to strip out the filename from the ImageSource variable. Those seem to be missing from VB 2013. I am used to VBA, so this is a whole new world for me. Here is my VB version of your code.

    	    Dim bm As BitmapImage
                Dim imageOrigin As Uri
                bm = sender.Source
                If Not (bm Is Nothing) Then
    
                    'This may or may not be set
                    imageOrigin = bm.UriSource
                    Debug.WriteLine("Origin: {0}", imageOrigin)
                End If

    Saturday, June 07, 2014 1:35 AM
  • St4mos, thanks for your help. I am sure that your solution may have worked, but I went with Rob's answer. 

    David

    Saturday, June 07, 2014 1:38 AM
  • Rob what you are saying its true. But if you know for sure that the sender is ImageControl you dont have to worry. 

    David I m not much into VB but this is the code in VB +to take the name from URI.

    I dont know if its good to use TryCast or DirectCast. But it works.

    Dim myImage = TryCast(DirectCast(sender, FrameworkElement), Image)
    Dim ImageUri As Uri = TryCast(myImage.Source, BitmapImage).UriSource
    Dim ImageNAme As String = ImageUri.Segments(ImageUri.Segments.Count - 1).Split("."c)(0)

     
    Saturday, June 07, 2014 1:30 PM
  • if you know for sure that the sender is ImageControl you dont have to worry.  

    As I mentioned in my previous post, your code will work only work if you can control that the Image's sources were all BitmapImages loaded from a Uri. If they are other ImageSources or loaded from streams then it will fail.

    If you're writing very specific one-off code then you can get away with it. If you are writing more general purpose code or giving general purpose advice then it is not a safe suggestion.

    --Rob

    Saturday, June 07, 2014 7:46 PM
    Owner