2012-06-13 253 views
5

我想通过一个Image绘制一些Rectangle在图像上绘制矩形

,比如我有以下的(白色和黑色)船的空间,我想在此配置文件中的特定位置增加一些(黄色和红色)矩形:

enter image description here

这可能吗?我怎样才能做到这一点?

回答

9

这很可能,如果您知道想要突出显示的区域的x,y,宽度和高度,则可以将所有控件放置到画布中。

可以在后面的代码上设置矩形的属性是这样的:

Rectangle rectangle = new Rectangle(); 
rectangle.SetValue(Canvas.LeftProperty, 10); 
rectangle.SetValue(Canvas.TopProperty, 10); 
rectangle.Width = 1000; 
rectangle.Height = 50; 
rectangle.Fill = new SolidColorBrush() { Color = Colors.Red, Opacity = 0.75f }; 

canvas.Children.Add(rectangle); 

,如果你想将它们添加在XAML中,你可以这样。

<Canvas> 
    <Image Source="..."/> 
    <Rectangle Canvas.Left="10" Canvas.Top="10" Width="1000" Height="50"> 
     <Rectangle.Fill> 
      <SolidColorBrush Color="Red" Opacity="0.75"/> 
     </Rectangle.Fill> 
    </Rectangle>       
</Canvas> 
+1

他还需要使与透明度的彩色输出。 – kenny

+0

@kenny是有可能吗? – Nick

+0

我已经用C#和XAML更新了答案,使rectange变成红色并且稍微透明。 – Andy

1

试试这个也会帮到你。

<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="Multi_Textbox.Window1" 
x:Name="Window" 
Title="Window1" 
Width="640" Height="480"> 

<Grid x:Name="LayoutRoot"> 
    <Image Margin="104,50,75,99" Source="barkship.jpg"/> 
    <Rectangle Fill="#FF28B0DE" HorizontalAlignment="Left" Height="17.334" Margin="212,0,0,111.333" Stroke="Black" VerticalAlignment="Bottom" Width="99.667"/> 
    <TextBlock HorizontalAlignment="Left" Height="11" Margin="230.667,0,0,115" TextWrapping="Wrap" Text="CHANDRU" VerticalAlignment="Bottom" Width="63.333" Foreground="White"/> 
</Grid> 

是这样的

Result

+0

这个讽刺是强1 +1 – sam