Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Guys I am working on silverlight application this application is Basically image edittin so I have Canvas and inside the canvas I have a two child one is Image and Second is Rectange so I am Panning the Image and my Canvas is Fix size so how i'll get the Rectangle Top Left Position.

I had tried with Canvas.GetTop(myRectangle); but this will give me the some wrong value and Canvas.GetLet(myRectangle); it is also give me the same value so how I can resolve this problem..!!



My XAML is like this..
<canvas>
     x:Name="myCanvas"
     Grid.Row="1"
      ScrollViewer.HorizontalScrollBarVisibility="Visible"
      ScrollViewer.VerticalScrollBarVisibility="Visible"
     Grid.Column="0"
     Width="{Binding ElementName=imgEdit,Path=ActualWidth}"
     Height="{Binding ElementName=imgEdit,Path=ActualHeight}"
     Grid.ColumnSpan="2"
     HorizontalAlignment="Center"
     VerticalAlignment="Center">


     <!--Width="490"
             Height="490"-->

     <Image x:Name="imgEdit"
             Cursor="Hand"
             Stretch="Uniform"
             MouseLeftButtonDown="imgEdit_MouseLeftButtonDown"
             MouseMove="imgEdit_MouseMove"
             MouseLeftButtonUp="imgEdit_MouseLeftButtonUp"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             RenderTransformOrigin="0.5, 0.5"
             >

             <Image.Effect>
                 <l:briconeffect xmlns:l="#unknown">
                         Brightness="{Binding ElementName=bVal, Path=Value}"
                         Contrast="{Binding ElementName=cVal, Path=Value}"
                         Gamma="{Binding ElementName=gVal,Path=Value}"
                         RedRatio="{Binding ElementName=rVal,Path=Value}"
                         BlueRatio="{Binding ElementName=blueVal,Path=Value}"
                         GreenRatio="{Binding ElementName=greenVal,Path=Value}"/>
             </Image.Effect>

             <Image.RenderTransform>

                 <transformgroup>
                     <rotatetransform x:name="Rotator" xmlns:x="#unknown">
                         <rotatetransform.angle>
                             <binding elementname="sldVerHorizontal" path="Value" mode="TwoWay" />
                         </rotatetransform.angle>
                     </rotatetransform>

                     <scaletransform x:name="Scale" xmlns:x="#unknown">
                         <scaletransform.scalex>
                             <binding elementname="sldZoomInOut" path="Value" mode="TwoWay" />
                         </scaletransform.scalex>
                         <scaletransform.scaley>
                             <binding elementname="sldZoomInOut" path="Value" mode="TwoWay" />
                         </scaletransform.scaley>
                     </scaletransform>
                 </transformgroup>
             </Image.RenderTransform>
         </Image>

     <border>
             Grid.Row="1"
             Grid.ColumnSpan="2"
             x:Name="maskBorder"
             BorderBrush="Green"
             BorderThickness="5"
             Width="200"
             Height="200"
             Cursor="Hand"
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             >
                 <!--Canvas.Top="150"
                 Canvas.Left="150"-->

         <rectangle>
                 Grid.Row="1"
                 Grid.ColumnSpan="2"
                 x:Name="maskRect"
                 Width="200"
                 Height="200"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 Cursor="Hand"
                 >
         </rectangle>
     </border>

 </l:briconeffect></canvas>
Posted
Updated 13-Jun-11 5:16am
v3
Comments
[no name] 11-Jun-11 15:23pm    
In my opinion the only thing you have is the canvas. Inside this canvas, canvas does not organize you anything…in other words you are responsible where your image is located and also your rectangle (what ever you mean with “…and a rectangle”….because there are a lot of possible rectangles inside a canvas, depending on it’s size ;) )

1 solution

Hello,

The following code works perfectly:

A very simple Silverlight application with a Canvas, two children, an image and a rectangle.

This is the Xaml:

XML
<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas Height="163" HorizontalAlignment="Left" Margin="54,69,0,0" Name="canvas1" VerticalAlignment="Top" Width="286">
            <Image Canvas.Left="30" Canvas.Top="25" Height="91" Name="image1" Stretch="Fill" Width="116" Source="/SilverlightApplication2;component/Images/diplodocus2.jpg" />
            <Rectangle Canvas.Left="186" Canvas.Top="68" Height="69" Name="rectangle1" Stroke="Black" StrokeThickness="1" Width="94" />
        </Canvas>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="208,259,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</UserControl>


Ans this is the code behind:

C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    foreach (var item in this.canvas1.Children)
    {
        double X = Canvas.GetLeft((UIElement)item);
    }
}


When the code executes Canvas.GetLeft returns the correct values.

Hope it helps.

Valery.
 
Share this answer
 
Comments
Jitendra.Jadav 13-Jun-11 11:13am    
Dear valery possoz: my question is like this if my image dimension is various so the I am fixing the Canvas size defualt Canvas.Top="150" and Canvas.Let="150" so I am getting always 150 of the Canvas top and left so how I can Resolve this problem. I am adding my XAML so you can see my modify question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900