Solved

Change the source of an image on button click

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

Accepted Solution

by:
Fernando Soto earned 300 total points
Comment Utility
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
Comment Utility
I get a namespace error.

What is the directive for PNG and JPG images?

I tried using System.Windows.Media.Imaging;

Thanks,
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:Dovberman
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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 62

Expert Comment

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

Author Comment

by:Dovberman
Comment Utility
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
Comment Utility
Very helpful.

Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
.
Not a Problem, always glad to help out.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now