jyoung
jyoung

Reputation: 5181

How do I set the absolute position of a WPF GeometryDrawing?

How do I set the absolute position of a GeometryDrawing? Currently the line is always drawn in the center. I guess the LineGeometery is always following the Alignments on Image control.

Here is some (now edited) code I am working with:

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

Upvotes: 1

Views: 2498

Answers (3)

jyoung
jyoung

Reputation: 5181

It looks like Vertical & HorizontalAlignment affects the internal drawing of a DrawImage. So depending on VerticalAlignment, a single horizontal line will be drawn at the top, center, or bottom of the final Image. I am now embedding the Drawing in a FrameworkElement with the needed absolute positioning.

Corrected per Liz's comment.

Upvotes: 1

Liz
Liz

Reputation: 8968

If you change Stretch="None" to Stretch="Fill" on your Image it will use the whole space. It won't fill up the whole size of the border, because you have specified a height and width.

Now if you use a GeometryGroup you can specify as many types of lines, paths, whatever. This will then be stretched to fill your image space.

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

The lines will be drawn to "fit" your image. Which is a little closer to what you want, I think. Leaving the the Stretch to None, will draw the objects at the center of the image, and will not stretch them when the image is re-sized.

That seems to be the two alternatives of using the ImageDrawing, either center, or draw to fit.

Upvotes: 0

Wonko the Sane
Wonko the Sane

Reputation: 10823

I believe that your problem (aside from the code above that does not compile, since Image.Source does not have Width and Height attributes) isn't that your GeometryDrawing is centered, it's that your Canvas is centered.

To prove it, just make the Background of your Canvas Red:

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

Your GeometryDrawing is at the appropriate position within this centered Canvas.

Geometry Drawing Example

If you want the Canvas to fit the entire window, don't set explicit width and height, set HorizontalAlignment and VerticalAlignment to Stretch:

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

to get:
Stretch Canvas

Upvotes: 1

Related Questions