1

I need to change the background image at runtime, based on which RadioButton the user clicks. I'm doing this in a WPF project in Visual Studio, and I need to put the code in the Checked event in the xaml.cs file

I have an Image control called imgBackground, with 6 images in its Source collection, which are listed in an Images folder in the Solution Explorer.

I've tried:

this.imgBackground.Source = "filename.jpg";

both with and without the quotes, and with various paths (I've tried too many different variations to list them all here) and nothing works - everything I've tried just gives an error in the editor, before I even try to build and run anything (the error given varies depending on what I'm trying at the time).

default
  • 11,485
  • 9
  • 66
  • 102
Erica
  • 65
  • 1
  • 3
  • 9
  • So, what doesn't work. The switching or displaying any image at all? You can use the designer+properties to select an image. Then look at the source. – H H Mar 22 '13 at 08:49
  • Sorry, I should have been clearer - I get errors right in the editor, before I even try to build and run anything. I've edited the original post to reflect that. Thanks! :-) – Erica Mar 22 '13 at 08:56
  • 1
    Show 1 attempt and post the err message – H H Mar 22 '13 at 09:00
  • What errors do you get? What does it say??? – bash.d Mar 22 '13 at 09:03
  • The error I get from my example is "Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource' " – Erica Mar 22 '13 at 09:25

1 Answers1

2

If you are using relative paths as filenames like

this.imgBackground.Source = "filename.jpg";

then these files must be in the same directory as the .exe of your program is. One workaround would be to use absolute paths like

this.imgBackground.Source = @"C:\MyFolder\MyProject\filename.jpg";

Or, even further use the packaging mechanism of WPF or pack your images as resources into your assembly. Look at this thread.

EDIT:

For your clarification:

The Source-property demands an System.Windows.Media.ImageSource-object, which you must provide. Do it like this:

BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("filename.jpg", UriKind.Relative);
bi3.EndInit();
this.imgBackground.Source = bi3;

Please refer to this documentation here.

Community
  • 1
  • 1
bash.d
  • 13,029
  • 3
  • 29
  • 42
  • They're in the Images folder of the Project. How do I go about accessing them from there? I should clarify (and I've edited the original post to reflect this as well) that it isn't even at the .exe stage - I'm getting errors right in the editor, before I even try to build and run anything. – Erica Mar 22 '13 at 08:59
  • So, then you'll need to specify the path to the images. If there is an images-folder, use `this.imgBackground.Source = @"images\filename.jpg";` – bash.d Mar 22 '13 at 09:01
  • 1
    @Erica, one thing to note is that even if they are in your images folder, your program will look in relation to the binary folder at runtime (i.e. it will look in bin/images instead of projectdir/images). You can fix this by right clicking on your images in Visual Studio, click "Properties" and setting "Copy to output directory" to true. This will ensure that they're copied into bin/images each time you build. – Doctor Jones Mar 22 '13 at 09:07
  • @bash.d - I tried your suggestion, this.imgBackground.Source = @"images\filename.jpg"; but it gives the error message "Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource' - what am I missing? – Erica Mar 22 '13 at 09:15
  • Ah, okay... Now I get it!! – bash.d Mar 22 '13 at 09:23
  • Okay, I inserted the code lines you suggested. When I build and run the program, the picture changes fine the first time I select a radiobutton, but if I select another to change it again the program crashes. There's no error message, it just says the program "has stopped working. Windows is checking for a solution to the problem." – Erica Mar 23 '13 at 02:47
  • Run the programm in debug-mode (press F5). It should display an exception then, when you select another radio-button. Post the message here, please. – bash.d Mar 23 '13 at 03:40
  • It's pointing to the bi3.BeginInit(); line, with the message "Cannot set the initializing state more than once." I have this line as the first line in the Checked event itself. I also tried putting it in each branch of the nested if statement, with the same error. Where should it go? And where should the EndInit() line then go? – Erica Mar 23 '13 at 04:28
  • You must not call `BeginInit()` more than once in a row! As soon as you called `BeginInit()` set your image and call `EndInit()`. Do it like `if(radiobutton == rb1){ BeginInit(); ... EndInit(); } else if (radiobutton == rb1) { BeginInit(); ... EndInit();} else { BeginInit(); ... EndInit(); }` – bash.d Mar 23 '13 at 07:09