Select to view content in your preferred language

Changing Photos in Code-behind

640
2
09-25-2012 05:18 AM
BrianLord1
Deactivated User
Hi, I want to be able to change a photo on a usercontrol dialog panel based on the item selected in a separate combo box.  Basically, when a user selects an item from the combo box a graphic is added to a graphics layer.  From there, I have been able to update all the other text boxes in the form by grabbing the attributes from specified fields for the selected graphic and using them to populate the test properties on the text boxes. 

For the image, I have been trying to change the source property based on the value in the attribute table for the selected graphic.I have a field (IMAGES) in the layer that contains the names of the image files.

Here is the code that does not work...
            Dim photoName As String = selectedFeaturePolling.Attributes.Item("IMAGES")
            Dim imageURLstring As String = String.Format("Torrens;component/Images/PollingPlaces/{0}'", photoName)

            PollingDialog.PollingPlaceImage.Source = New BitmapImage(New Uri(imageURLstring, UriKind.Relative))


Through the use of message boxes I know that the correct file name is grabbed from the attribute table in the first line and that it is properly added into the URL string on the second line.

The third line is where I get lost.  I found this example online, but nothing happens when I run this code.

Any help would be greatly appreciated.

Thanks,
Mark
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
You can use GetResourceStream to load an Embedded resource.
Something like

StreamResourceInfo sr = Application.GetResourceStream(new UriString.Format("Torrens;component/Images/PollingPlaces/{0}'", photoName), UriKind.Relative));
 BitmapImage bmp = new BitmapImage();
 bmp.SetSource(sr.Stream);
0 Kudos
BrianLord1
Deactivated User
Thanks for the help.

I actually got this to work using a different bit of code (as shown below).

           Dim photoName As String = selectedFeaturePolling.Attributes.Item("IMAGES")
            Dim uriString As String = String.Format("../PollingPlaces/{0}", photoName)

            Dim photo As Image = New Image
            photo.Source = New BitmapImage(New Uri(App.Current.Host.Source, uriString))

            PollingDialog.PollingPlaceImage.Source = photo.Source


Also, while debugging I saw what UriSource was actually being passed in as the image.source.  I then realized that my photos folder was not even there...

Which brings me to the questions I probably should have asked first.  Where in my solution should I save the photos and should I set their build action to "Resources" or "Content"?

Thanks again,
Mark
0 Kudos