C# 在图像上绘制叠加层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/743801/
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-08-04 23:27:29  来源:igfitidea点击:

Draw overlay on an image

c#wpfxamldrawingoverlay

提问by Yuval Peled

I have an image that the user can zoom/scroll. I want to draw some rectangles/circles on a different layer (for example: drawing a circle for each person's face that was identified in the picture).

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

The rectangle position is relative to the image.

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

How do I create such an overlay?

如何创建这样的叠加层?

采纳答案by idursun

I have managed to do something similar:

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

  • Set image as background
  • Put a transparent ItemsControlon top of it
  • Set ItemsControl.ItemsPanelto Canvas
  • wrote handlers for dragging operations
  • 将图像设置为背景
  • ItemsControl在上面放一个透明的
  • 设置ItemsControl.ItemsPanelCanvas
  • 为拖动操作编写了处理程序

Code Snippet:

代码片段:

  <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>

回答by Rob

An easy way is to just use a Canvas and set the canvas' background property to your photo, and then place your circles or rectangles on top of that and position them with the Canvas.Left and .Top properties.

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