Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Change the source of an image on button click

Posted on 2014-01-18
9
700 Views
Last Modified: 2014-11-12
I need to change the source of an image on button click.

The compie error I am getting is:

Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource'

My code:

void cmdHole1_Tap(object sender,
                     System.Windows.Input.GestureEventArgs e)
            // Place image into imgHole1
            {
                string strImageSource = "C:\\Develop\\Apps\\Holdem\\Images\\cards_png\\C1Full.png";
                imgHole1.Source = strImageSource;
                int intTest1 = 1;

            }

What should I change?

Thanks
0
Comment
Question by:Dovberman
  • 5
  • 4
9 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 39791141
Hi Dovberman;

The following code snippet should do what you need.

void cmdHole1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
// Place image into imgHole1
{
    string strImageSource = "C:\\Develop\\Apps\\Holdem\\Images\\cards_png\\C1Full.png";
    BitmapImage bmi = new BitmapImage();
    bmi.BeginInit();
    bmi.UriSource = new Uri(strImageSource, UriKind.Absolute);
    bmi.EndInit();
    imgHole1.Source = bmi;
    int intTest1 = 1;
}

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39791296
I get a namespace error.

What is the directive for PNG and JPG images?

I tried using System.Windows.Media.Imaging;

Thanks,
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39791393
I had tested my code using a jpg image file and did not have a problem using the namespace System.Windows.Media.Imaging.

What the exact exception and inner exception if any.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Dovberman
ID: 39791465
This code does not produce errors:

               string strImageSource = "C:\\Develop\\Apps\\Holdem\\Images\\cards_png\\C1Full.png";
                BitmapImage bmi = new BitmapImage();
                bmi.UriSource = new Uri(strImageSource, UriKind.Absolute);
                imgHole1.Source = bmi;

This threw errors:

bmi.BeginInit();

Error      2      'System.Windows.Media.Imaging.BitmapImage' does not contain a definition for 'BeginInit' and no extension method 'BeginInit' accepting a first argument of type 'System.Windows.Media.Imaging.BitmapImage' could be found (are you missing a using directive or an assembly reference?)      C:\Users\David\documents\visual studio 2012\Projects\PhoneApp1\PhoneApp1\MainPage.xaml.cs      67      21      PhoneApp1

Is it OK to refer to a bitmap when actually using PNG images?

Thanks,
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39791499
I was not aware that this was a Windows Phone application because of that Windows Phone does not support the BeginInit and EndInit methods so just remove those two lines from the  code snippet that I posted and try it.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39791680
To your question, "s it OK to refer to a bitmap when actually using PNG images?", yes.
0
 

Author Comment

by:Dovberman
ID: 39791704
Thank you, The emulator does not render the image, but the event is properly triggered. Testing has been difficult.

This works now.

You have been very helpful.
0
 

Author Closing Comment

by:Dovberman
ID: 39791705
Very helpful.

Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39791719
.
Not a Problem, always glad to help out.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question