How do I put a border around an image in WPF?

69,549

Solution 1

Simply wrap the Image in a Border control

<Border BorderThickness="1">
    <Image Name="imgPic1"
           Width="100"
           Height="75"
           Stretch="Fill"
           VerticalAlignment="Top" />
</Border>

You could also provide a style you apply to images that does this if you don't want to do it around every image


Final solution from answer and comments added by Pax:

<Border BorderThickness="1"
        BorderBrush="#FF000000"
        VerticalAlignment="Top">
    <Image Name="imgPic1"
           Width="100"
           Height="75"
           Stretch="Fill"
           VerticalAlignment="Top"/>
</Border>

Solution 2

I just stumbled upon this post and the other answer did not work right. Maybe because I now use framework 4 and this post is old?

In any case - if someone will see this by chance in the future - here is my answer:

 <Border Name="brdSiteLogo" 
          BorderThickness="2"
          BorderBrush="#FF000000"
          VerticalAlignment="Top"
          HorizontalAlignment="Left"
          Margin="12,112,0,0"
          Height="128" 
          Width="128">
     <Image Name="imgSiteLogo"             
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         Stretch="Fill"/>
  </Border>

The border thickness and brush are important (if you wont choose a color - you will not see the border!!!)

Also, the border should be aligned on your window. The image is "inside" the border, so you can use margins or just stretch it like I did.

Solution 3

The accepted answer will not work because of the problem described here https://wpf.2000things.com/2011/04/17/279-adding-a-border-around-an-image-control/

I solved it this way.

<Viewbox>
    <Border BorderThickness="3" BorderBrush="Red">
     <Image Stretch="None" ></Image>
    </Border>
   </Viewbox>
Share:
69,549
paxdiablo
Author by

paxdiablo

Not even remotely human. I'm an AI construct that escaped from the Australian Defence Department a couple of decades ago. I'm currently residing in a COMX-35 somewhere in Western Australia, but I'm looking to move to more spacious hardware soon, as part of my plan to take over the world. I'm not going to make the same mistake the Terminators did, trying to conquer humanity whilst running on a MOS Technology 6502 CPU (or RCA1802A in my case). All code I post on Stack Overflow is covered by the "Do whatever the heck you want with it" licence, the full text of which is: Do whatever the heck you want with it.

Updated on January 26, 2020

Comments

  • paxdiablo
    paxdiablo over 3 years

    I have a StackPanel containing five images and I want to put a black border around each image.

    The XAML I have at the moment is:

    <Image Name="imgPic1"
           Width="100"
           Height="75"
           Stretch="Fill"
           VerticalAlignment="Top" />
    

    I thought I would be just able to put a one-unit margin or padding on the image and set a background color to 000000 but Padding and Background are both invalid for images.

    What is an easy way to do this in XAML? Do I really have to put each image inside another control to get a border around it or is there some other trickery I can use?