Skip to main content

Microsoft Silverlight

Unanswered Question Image goes out of boundry while draggingRSS Feed

(0)

Cute_Raj
Cute_Raj

Member

Member

31 points

58 Posts

Image goes out of boundry while dragging

Hi,

I am developing an application where I need to drag and zoom an image. Zooming is acheived using slider control and following is the code for that,

 

 <ScrollViewer x:Name="scrlViewer" HorizontalScrollBarVisibility="Hidden" Margin="0,0,0,0" VerticalScrollBarVisibility="Hidden" BorderThickness="3,3,3,3" BorderBrush="{x:Null}" >
        	<Canvas x:Name="cvsImage">
                <Canvas.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="CanvasScaleTransform" ScaleX="1" ScaleY="1"/>
                        <TranslateTransform x:Name="CanvastranslateTransform"/>
                    </TransformGroup>
                </Canvas.RenderTransform>
                <Canvas x:Name="cvsIn">
        	<Image Canvas.Top="0" Source="quince-blooms-with-bee-JJwe.jpg" Stretch="UniformToFill" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="390" Width="929" RenderTransformOrigin="0.5,0.5"/>
                </Canvas>
        	</Canvas>
        </ScrollViewer>    

 and C# is

 

 private void sliderZoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            st.ScaleX = sliderZoom.Value;
            st.ScaleY = sliderZoom.Value;

            cvsIn.RenderTransform = st;
            cvsIn.Height = cvsImage.ActualHeight * sliderZoom.Value;
            cvsIn.Width = cvsImage.ActualWidth * sliderZoom.Value;
         }
 
for dragging the image,
  
 void cvsImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseCapture)
            {
                Pan(e.GetPosition(scrlViewer));
            }
        }
        private void Pan(Point point)
        {
            CanvastranslateTransform.X = point.X - ClickPosition.X * CanvasScaleTransform.ScaleX;
            CanvastranslateTransform.Y = point.Y - ClickPosition.Y * CanvasScaleTransform.ScaleY;

         } 
 

My problem is when ever I drag this image it completly goes out of the screen. So i need to check a condition before moving the canvas.

 

I don't know how to check that.

 

Please help me

 

 

K2P2
K2P2

Participant

Participant

1028 points

337 Posts

Re: Image goes out of boundry while dragging

Does it make sense to do something like the following:

Double X = point.X - ClickPosition.X * CanvasScaleTransform.ScaleX;
Double Y = point.Y - ClickPosition.Y * CanvasScaleTransform.ScaleY;

If ( X < something  && Y < something )
{
   CanvastranslateTransform.X = X;
   CanvastranslateTransform.Y = Y
}

Cute_Raj
Cute_Raj

Member

Member

31 points

58 Posts

Re: Image goes out of boundry while dragging

Yes I tried this too. But on what basis should I need to compare the X and Y value.

paru
paru

Member

Member

164 points

28 Posts

Re: Image goes out of boundry while dragging

Get the position of your image in the canvas that vsets the X and Y

Thanks
Paru

--mark as answer if helped

K2P2
K2P2

Participant

Participant

1028 points

337 Posts

Re: Image goes out of boundry while dragging

Sorry, I've been away a while.

As I understand your problem you might be able to use the following:

System.Windows.Interop.Content interopContent = new System.Windows.Interop.Content ();

currentBrowserWidth = interopContent.ActualWidth;
currentBrowserHeight = interopContent.ActualHeight;

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities