Solved

Change the source of an image on button click

Posted on 2014-01-18
9
709 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 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
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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