Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change the source of an image on button click

Posted on 2014-01-18
9
Medium Priority
?
752 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
[X]
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
  • 5
  • 4
9 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1200 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 64

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 64

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 64

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 64

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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