Add an image in a WPF button

wpf button image from resource
wpf button with image and text
wpf image button style
wpf button background image
button inside image wpf
how to add image to button in visual studio
wpf image click event
wpf button image png

I tried this solution:

<Button>
    <StackPanel>
        <Image Source="Pictures/img.jpg" />
        <TextBlock>Blablabla</TextBlock>
    </StackPanel>
</Button>

But I can see the image only in the project window, and when I launch the program it disappears.

If I try this:

  Image img = new Image();
  img.Source = new BitmapImage(new Uri("foo.png"));

  StackPanel stackPnl = new StackPanel();
  stackPnl.Orientation = Orientation.Horizontal;
  stackPnl.Margin = new Thickness(10);
  stackPnl.Children.Add(img);

  Button btn = new Button();
  btn.Content = stackPnl;

I get a "'System.Windows.Markup.XamlParseException' in PresentationFramework.dll" exception.

What is the solution?

Add an image in a WPF button, The following example creates two Button controls. One Button contains text and the other contains an image. The image is in a folder called  One Button contains text and the other contains an image. The image is in a folder called data, which is a subfolder of the example’s project folder. When a user clicks the Button that has the image, the background and the text of the other Button change.

Please try the below XAML snippet:

<Button Width="300" Height="50">
  <StackPanel Orientation="Horizontal">
    <Image Source="Pictures/img.jpg" Width="20" Height="20"/>
    <TextBlock Text="Blablabla" VerticalAlignment="Center" />
  </StackPanel>
</Button>

In XAML elements are in a tree structure. So you have to add the child control to its parent control. The below code snippet also works fine. Give a name for your XAML root grid as 'MainGrid'.

Image img = new Image();
img.Source = new BitmapImage(new Uri(@"foo.png"));

StackPanel stackPnl = new StackPanel();
stackPnl.Orientation = Orientation.Horizontal;
stackPnl.Margin = new Thickness(10);
stackPnl.Children.Add(img);

Button btn = new Button();
btn.Content = stackPnl;
MainGrid.Children.Add(btn);

How to: Create a Button That Has an Image, In this WPF Tutorial, I will show you how to add image inside the button control. I will give Duration: 3:13 Posted: May 2, 2014 WPF Button with Image. < Button Name = "AButton" Width = "50" Height = "30" Click = "OnImageButtonClick" >. < Image Source = "data\YourImageName.jpg" > </ Image > </ Button >. < Button Name = "Button2" BorderBrush = "Black" > Click the picture. </ Button >.

Use:

<Button Height="100" Width="100">
  <StackPanel>
    <Image Source="img.jpg" />
    <TextBlock Text="Blabla" />
  </StackPanel>
</Button>

It should work. But remember that you must have an image added to the resource on your project!

How to put an image into a button control in wpf, This is Part-11 of WPF series. And this tutorial will guide you to How to add Image into Button Duration: 7:56 Posted: Feb 11, 2017 Set image in a button in wpf. Set image in a button in wpf. Skip navigation Sign in. Search. C# WPF Tutorial 2- Adding Image to WPF C# application (Image control , background image) - Duration

You can set the button's background to the image if you then want to overlay text.

<Button>
   <Button.Background>
     <ImageBrush ImageSource="/AssemblyName;component/Pictures/img.jpg"/>
   </Button.Background>
   <TextBlock>Blablabla</TextBlock>
</Button>

Watch out for the image source syntax. See this question for help.

Mastering WPF || How to Add Image in Button wpf || In Urdu/Hindi , Putting image inside button control using stack panel. 2. How to change background color of Duration: 5:26 Posted: Jul 9, 2016 Hoe to Add multiple Images to Canvas on button click event in wpf Sergey Alexandrovich Kryukov 4-Feb-16 1:37am Just add some instances of Image elements as children of Canvas using Canvas.Children.Add, Canvas.SetTop and Canvas.SetLeft to position each one.

I also had the same issue. I've fixed it by using the following code.

        <Button Width="30" Margin="0,5" HorizontalAlignment="Stretch"  Click="OnSearch" >
            <DockPanel>
                <Image Source="../Resources/Back.jpg"/>
            </DockPanel>
        </Button>

Note: Make sure the build action of the image in the property window, should be Resource.

Image Button Control in Windows Presentation Foundation, <Button> <DockPanel> <Image Source="image.png"/> <Text DockPanel.Dock="​fill" VerticalAlignment="center"> Button <Italic>with Image! The XAML code looks like this: < StackPanel Orientation = "Horizontal" Background = "LightBlue" Height = "40" >. < Label Margin = "10,0,0,0" Height = "23" Name = "Label1" >. Current File: </ Label >. < Label Margin = "5,0,0,0" Height = "25" Name = "FileNameLabel" Width = "300" />. < Button Margin =

How to add icon in WPF Button, It sounds like you answered your own question. If you need to add dependency properties, then you're not doing that in XAML but adding  How to add Image and Button in wpf DataGrid. How to design an icon button in Expression Blend. How to add icons to MFC ribbon buttons.

#341 – Create a Button with an Image and Text, The only way I can get it to add the file as an image and therefore put it in a separate folder like your screenshot is if I add a .png, .jpg., .gif, .bmp or .tiff image file. If I  Add basic default buttons: All the files you need for this walkthrough are provided by the template. Open the Window1.xaml file by double clicking it in Solution Explorer. By default, there is a Grid element in Window1.xaml. Remove the Grid element and add a few buttons to the Extensible Application Markup Language (XAML) page by typing or copy and pasting the following highlighted code to Window1.xaml:

Adding a xaml image to a button in WPF Solutions, The WPF Image control will allow you to display images inside your applications. As you probably know, you can add resource files to your project - they can exist inside Click="BtnLoadFromResource_Click">Load from Resource</​Button> In this WPF Tutorial, I will show you how to add image inside the button control. I will give you a demo of image with button.

Comments
  • Is your image defined as 'Resource' in it's properties? (right click it -> properties -> Build Action='Resource')
  • Thanks! You put me in the right direction: I drag and dropped the Image in Solution Explorer and now I can see it :)
  • I think you will have to put double quotes like <Button Content="{StaticResource MyImage}" /> rather than content={...}.
  • the source path can try <Image x:Key="kkk" Source="../Folder1/Img.png"/>
  • I tried your second method but stuck with the this error. "The attachable property 'Resources' was not found in type 'UserControl'."
  • what should I fill for sometemplate ?
  • if you're not using Template, you can remove Template="{StaticResource SomeTemplate}", if you want to know what template is, you can try to google wpf template