在图像上绘制叠加层

时间:2009-04-13 12:47:21

标签: c# wpf xaml drawing overlay

我有一个用户可以缩放/滚动的图像。 我想在不同的图层上绘制一些矩形/圆形(例如:为图中标识的每个人的脸画一个圆圈。)

矩形位置相对于图像。

如何创建这样的叠加层?

2 个答案:

答案 0 :(得分:6)

一种简单的方法是使用Canvas并将画布的背景属性设置为您的照片,然后将圆圈或矩形放在其上并使用Canvas.Left和.Top属性定位它们。

    <Canvas x:Name="myCanvas">
        <Canvas.Background>
            <ImageBrush ImageSource="c:\photo.bmp"/>
        </Canvas.Background>
        <Image Canvas.Top="20" Canvas.Left="20" Height="20" Width="20" Source="c:\circle.bmp"/>
    </Canvas>

答案 1 :(得分:5)

我设法做了类似的事情:

  • 将图片设置为背景
  • 在其上放置一个透明的ItemsControl
  • ItemsControl.ItemsPanel设为Canvas
  • 编写了用于拖动操作的处理程序

代码段:

  <ItemsControl x:Name="overlayItemsControl" 
        Background="Transparent"  
        ItemsSource="{Binding Path=Blocks}"
        Width="{Binding ElementName=imageControl, Path=Width}"
        Height="{Binding ElementName=imageControl, Path=Height}"
        ItemContainerStyle="{StaticResource rectStyle}"
        PreviewMouseMove="ItemsControl_PreviewMouseMove"
        PreviewMouseDown="ItemsControl_PreviewMouseDown"
        PreviewMouseUp="ItemsControl_PreviewMouseUp"
        PreviewKeyDown="ItemsControl_PreviewKeyDown">

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
   ....
</ItemsControl>
相关问题