2010-12-21 97 views
1

如何设置GeometryDrawing的绝对位置? 目前该线总是绘制在中央。 我想LineGeometery总是遵循图像控件上的对齐。如何设置WPF GeometryDrawing的绝对位置?

这里是我一起工作的一些(现在编辑)代码:

<Window x:Class="WpfProblem.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" > 
<Grid Width="450" Height="160" Background="Beige" VerticalAlignment="Stretch"> 
    <Border BorderBrush="Lime" BorderThickness="1" CornerRadius="80" Background="#1AFF0000" Margin="0"> 
     <Image Width="420" Height="120" Stretch="None" > 
      <Image.Source> 
       <DrawingImage> 
        <DrawingImage.Drawing> 
         <GeometryDrawing> 
          <GeometryDrawing.Geometry> 
           <LineGeometry StartPoint="0,30" EndPoint="299,30"/> 
          </GeometryDrawing.Geometry> 
          <GeometryDrawing.Pen> 
           <Pen Thickness="5" Brush="Black"/> 
          </GeometryDrawing.Pen> 
         </GeometryDrawing> 
        </DrawingImage.Drawing> 
       </DrawingImage> 
      </Image.Source> 
     </Image> 
    </Border> 
</Grid> 
</Window> 
+0

难道你不只是设置你已经在'`标签上定义的Canvas.Left和Canvas.Top属性? – 2010-12-21 20:55:37

+0

Canvas.Top&Left没有帮助。问题是LineGeometery正在图像控件中对齐。 – jyoung 2010-12-21 22:07:34

回答

1

它看起来像垂直& Horizo​​ntalAlignment影响DrawImage的内部绘图。因此,根据VerticalAlignment,将在最终图像的顶部,中间或底部绘制一条水平线。我现在将绘图嵌入到具有所需绝对位置的FrameworkElement中。

根据Liz的评论更正。

1

我相信您的问题(除了上面说的代码不能编译,因为Image.Source没有宽度和高度属性)并不是说你的GeometryDrawing居中,而是你的Canvas居中。

为了证明这一点,只是让你的画布红色的背景:

<Canvas Width="350" Height="180" Background="Red"> 

你GeometryDrawing是在这个中心的帆布适当的位置。

Geometry Drawing Example

如果你想在画布,以适应整个窗口,不设置明确的宽度和高度,设置的Horizo​​ntalAlignment和VerticalAlignment为Stretch:

<Canvas HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch" 
     Background="Blue"> 

获得:
Stretch Canvas

+0

即使使用您的建议,该行仍然在图像控件中居中。 – jyoung 2010-12-21 22:09:19

+0

嗯......我复制了你的原始代码,编写了它,编辑了上面的屏幕截图。我将无法访问该代码一周左右,但我看到您仍然设置绝对大小。发布你的代码产生的屏幕截图,确保与任何编辑的屏幕截图相匹配。 – 2010-12-22 02:31:49

0

如果在图像上将Stretch =“None”更改为Stretch =“Fill”,它将使用整个空间。它不会填满整个边框的大小,因为您已指定了高度和宽度。

现在,如果您使用GeometryGroup,则可以指定任意类型的线条,路径等等。这将被延伸,以填补你的图像空间。

<GeometryGroup> 
    <LineGeometry StartPoint="0,0" EndPoint="410,0"/> 
    <LineGeometry StartPoint="0, 100" EndPoint="410, 0"/> 
</GeometryGroup> 

这些线条将被绘制为“适合”您的图像。 我认为,这更接近你想要的东西。将“拉伸”保留为“无”,将在图像中心绘制对象,并且在图像重新调整大小时不会拉伸它们。

这似乎是使用ImageDrawing,无论是中心,或绘制拟合的两种选择。