0

I am new to C# programming so please have some mercy. I use Visual Studio Community 2019. I am in the process of creating a WPF App (so an .exe file can be created). Within the designer I have placed a number of buttons with images (see code below). I want to change that image in the code with "button.Image" as I have read in several solutions, but I get an error

(CS0246 The type or namespace name 'button_1_PL' could not be found (are you missing a using directive or an assembly reference?)).

As most solutions I have read are using VS 2013, I have tried installing that also, but to no avail (same errors). I have added "using System.Drawing;" to the header part of the code, but still get the same error. When I try to add "using System.Drawing.Image;" or "using System.Drawing.Imaging;" I get an error (CS0234 The type or namespace name 'Imaging' does not exist in the namespace 'System.Drawing' (are you missing an assembly reference?)).

The header part of c#:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

The header part of the designer:

<Window x:Class="mJingles.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:mJingles"
        mc:Ignorable="d"
        Title="mJingles - All your songs and jingles at your fingertips..." Height="1015" Width="1920">
    <Grid>
    <Button Name="button_1_PL" HorizontalAlignment="Left" Margin="98,85,0,0" VerticalAlignment="Top" Width="124" BorderThickness="0" Click="Button_1_PL_Click">
        <Image Source="Images\SelectPL.png"></Image>
    </Button>

I want to be able to change the background image of a button, but with all the examples I have found I still get the errors as described above. I am sure I must be missing something (in both senses of the word) but I don't know what.

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • 1
    In fact you want to change the _Source_ of the `Image` - not the `Button`. What code do you use in code behind to change the image? – schlonzo Jun 05 '19 at 12:46
  • Something like `((Image)button_1_PL.Content).Source = new BitmapImage(new Uri(...));` should work. The image files should be assembly resources (by adding them to the Visual Studio project and setting their Build Action to Resource). The Uri would then by a Resource File Pack URI like `"pack://application:,,,/Images/SelectPL.png"`. – Clemens Jun 05 '19 at 12:54
  • @schlonzo: I have no code yet, I have tried "button.Image" but that generated the error that caused me to ask this question. – Theo Scholten Jun 05 '19 at 14:08
  • 1
    @Clemens: That indeed dit the trick I was looking for. The examples on internet just showed me the wrong way appearantly. Thanks a lot. – Theo Scholten Jun 05 '19 at 14:09

0 Answers0