?
Solved

Change the source of an image on button click

Posted on 2014-01-18
9
Medium Priority
?
740 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 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 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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