在图像上绘制叠加层

4

我有一张用户可以缩放/滚动的图片。

我想在不同的层上绘制一些矩形/圆形(例如:为识别出图片中每个人脸绘制一个圆形)。

矩形的位置相对于图像。

如何创建这样的叠加层?

2个回答

7

一种简单的方法是使用 Canvas,将 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>

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接