【问题标题】:Draw overlay on an image在图像上绘制叠加层
【发布时间】:2009-04-13 12:47:21
【问题描述】:

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

矩形位置是相对于图像的。

如何创建这样的叠加层?

【问题讨论】:

    标签: c# wpf xaml drawing overlay


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我已经设法做了类似的事情:

      • 将图片设置为背景
      • 在上面放一个透明的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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 2019-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        相关资源
        最近更新 更多