问题描述
/*图片放大后,拖动图片,不想让Border里有余白。换句话说,如果图片向上拖,那么一旦图片的下边和Border的下边吻合后,图片就不能再网上拖了。求教各位该怎么做*/我根据了放大陪数来判断了,还是有bug/*前台*/<Windowx:Class="WPFImagePanAndZoom.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"x:Name="WPFWindow"Title="WPFPanAndZoom"mc:Ignorable="d"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Height="610"Width="566"><Gridx:Name="LayoutRoot"><Grid.RowDefinitions><RowDefinitionHeight="152.92"/><RowDefinitionHeight="*"/></Grid.RowDefinitions><BorderGrid.Row="1"Name="border"ClipToBounds="True"Width="800"Height="900"><ImageName="image"Opacity="1"Source="/WPF%20Image%20Pan%20and%20Zoom;component/Images/test.tif"/></Border><LabelContent="Mousewheeltozoom.Holdleftmousebuttontopan."Height="35"HorizontalAlignment="Center"Margin="123,12,128,0"Name="label1"VerticalAlignment="Top"Width="293"/></Grid></Window>/*C#*////<summary>///InteractionlogicforMainWindow.xaml///</summary>publicpartialclassMainWindow:Window{privatePointorigin;privatePointstart;publicMainWindow(){InitializeComponent();WPFWindow.MouseWheel+=MainWindow_MouseWheel;image.MouseLeftButtonDown+=image_MouseLeftButtonDown;image.MouseLeftButtonUp+=image_MouseLeftButtonUp;image.MouseMove+=image_MouseMove;}privatevoidimage_MouseLeftButtonUp(objectsender,MouseButtonEventArgse){image.ReleaseMouseCapture();}privateboolmouseDown;privatePointmouseXY;privatevoidimage_MouseMove(objectsender,MouseEventArgse){/*图片放大后,拖动图片,不想让Border里有余白。换句话说,如果图片向上拖,那么一旦图片的下边和Border的下边吻合后,图片就不能再网上拖了。求教各位该怎么做*/if(!image.IsMouseCaptured)return;Pointp=e.MouseDevice.GetPosition(border);Matrixm=image.RenderTransform.Value;doubleoffsetx=origin.X+(p.X-start.X);//m.OffsetX=origin.X+(p.X-start.X);doublec=0-(origin.X+(p.X-start.X));if(offsetx<=0&&c<=(border.Width*ScaleX))//在这里判断过可是不太行{if(c!=0){m.OffsetX=offsetx;}//transform.X+=position.X-m_PreviousMousePoint.X;}doubleOffsetY=origin.Y+(p.Y-start.Y);doubled=0-(origin.Y+(p.Y-start.Y));if(OffsetY<=0&&d<=border.Height)//在这里判断过可是不太行{m.OffsetY=OffsetY;}//m.OffsetY=origin.Y+(p.Y-start.Y);image.RenderTransform=newMatrixTransform(m);}privatevoidimage_MouseLeftButtonDown(objectsender,MouseButtonEventArgse){if(image.IsMouseCaptured)return;image.CaptureMouse();start=e.GetPosition(border);origin.X=image.RenderTransform.Value.OffsetX;origin.Y=image.RenderTransform.Value.OffsetY;}doublei=1;doubleScaleX=1;doubleScaleY=1;privatevoidMainWindow_MouseWheel(objectsender,MouseWheelEventArgse){Pointp=e.MouseDevice.GetPosition(image);Matrixm=image.RenderTransform.Value;if(e.Delta>0){i=i*2;ScaleX+=ScaleX*1.1;ScaleY+=ScaleY*1.1;m.ScaleAtPrepend(1.1,1.1,p.X,p.Y);}else{i=i/2;if(i>=1){ScaleX+=ScaleX/1.1;ScaleY+=ScaleY/1.1;m.ScaleAtPrepend(1/1.1,1/1.1,p.X,p.Y);}else{i=1;ScaleX=1;ScaleY=1;}}image.RenderTransform=newMatrixTransform(m);}}
解决方案
解决方案二:
上图吧
解决方案三:
贴出你具体要的效果图吧