views:

35

answers:

1

While displaying images in a silverlight application, I need to overlay a set of shapes likne lines exactly over the image. The line X1Y1 and X2 Y2 should be set by clicking mouse on image locations.

An image drawn like this:

and the canvas in the same grid cell for the shapes is like this:

    <Canvas Name="LeftLines" Grid.Row="1" Grid.Column="0">
        <Line Name="LeftTape" X1="20" X2="20" Y1="20" Y2="512" Stroke="Yellow" StrokeThickness="2" Visibility="Visible"></Line>
    </Canvas>

I can't figure out how to render the canvas on top of the image. I tried to use TransformGroup tg and then LefttLines.SetValue(Canvas.RenderTransformProperty, tg);

But something is missing.

Thanks for advice or examples. Val

+1  A: 

Part 2

Aha. Thanks for the extra info. If you are happy for the image to retain a uniform aspect ratio you can put the image and the lines in a grid within a ViewBox within your grid cell (canvas not required).

alt text

alt text

Part 1

I am not sure I understand the problem. The canvas origin is relative to the same cell and should be rendered fine. The order of the items in the XAML is important as that determines the render order (last item in the XAML is normally rendered last).

If you set the start and end co-ordinates of the line, in pixels, it should appear where you want it (relative to the grid cell the canvas is in, therefore on top of the image in the grid cell).

This sample XAML resulted in the image below it (with your line on top of an image):

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image Source="images[1].jpg" Grid.Row="1"/>
        <Canvas x:Name="LeftLines" Grid.Row="1" Grid.Column="0">
            <Line x:Name="LeftTape" X1="20" X2="167" Y1="20" Y2="142" Stroke="Yellow" StrokeThickness="2" Visibility="Visible"/>
        </Canvas>
    </Grid>

alt text

Enough already
Thanks for you quick responce.However, this works only if you Stretch="Fill".I use "Uniform" becasue I need to preserve the image proportions when resizing grid cells. the canvas is trully draws on top of the grid cell entirely. But when I resize the grid with gridsplitter the image zooms in / out and the canvas stays the same. I think I have to compute the transform of the image and apply it to the canvas somehow.the main challenge is to enable canvas children (like the line) to be attached to image and follow it with resizing. and, I need to get mouse click coords to set xy's
val
@val: I have updated the answer to support scaling. If you ever do need a non-fixed aspect ratio it would require code-behind to scale the line to match the image scale separately for the X and Y scales. Hope that does the trick for you.
Enough already
The ViewBox! of cause! I put the image and the line in a grid as two children of a viewbox: <Viewbox Stretch="Uniform" Grid.Row="1" Grid.Column="0"> <Grid> <Image x:Name="LeftImage" Source="lefttest.jpg"/> <Line Name="LeftTape" X1="20" X2="167" Y1="20" Y2="142" Stroke="Yellow" StrokeThickness="2" Visibility="Visible"/> </Grid></Viewbox>The rest is is easy and will be handled in the code.Thanks for the idea buddy.Cheers, Val
val
Hi Val, Glad I could help. You might want to complete your registration as I see you are asking a lot of questions, but are not accepting any answers. With a 0% acceptance rate the answers you receive will probably stop soon.
Enough already