如何在 WPF 中的图像周围放置边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3092992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:52:53  来源:igfitidea点击:

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

wpfxaml

提问by paxdiablo

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

我有一个StackPanel包含五个图像的图像,我想在每个图像周围放置一个黑色边框。

The XAML I have at the moment is:

我目前拥有的 XAML 是:

<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 000000but Paddingand Backgroundare both invalid for images.

我以为我只能在图像上放置一个单位的边距或填充,并将背景颜色设置为000000Padding并且Background对图像均无效。

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?

在 XAML 中执行此操作的简单方法是什么?我真的必须将每个图像放在另一个控件中才能在它周围设置边框还是我可以使用其他一些技巧?

回答by Craig Suchanec

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:

来自 Pax 添加的答案和评论的最终解决方案:

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

回答by Guy Cohen

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?

我只是偶然发现了这篇文章,而另一个答案不起作用。也许是因为我现在使用框架 4 而这篇文章很旧?

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.

此外,边框应该在您的窗口上对齐。图像在边框“内部”,因此您可以使用边距或像我一样拉伸它。

回答by Andreas

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/

由于此处描述的问题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>