我有一张Canvas with Image。
<Canvas
x:Name="ImageCanvas"
SizeChanged="Root_OnSizeChanged">
<Canvas.Clip>
<RectangleGeometry
x:Name="RootGeometry"/>
</Canvas.Clip>
<Image
x:Name="Image"
Source="{Binding TestData, Converter={StaticResource TestDataToBitmapConverter} }"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ManipulationStarted="OnManipulationStarted"
ManipulationDelta="OnManipulationDelta"
ManipulationCompleted="OnManipulationCompleted"
Stretch="None"
ImageOpened="OnImageOpened">
<Image.RenderTransform>
<TransformGroup>
<MatrixTransform x:Name="previousTransform" />
<TransformGroup x:Name="currentTransform">
<ScaleTransform x:Name="scaleTransform" />
<RotateTransform x:Name="rotateTransform" />
<TranslateTransform x:Name="translateTransform" />
</TransformGroup>
</TransformGroup>
</Image.RenderTransform>
</Image>
</Canvas>
当我在屏幕上移动图像时,它仍然可能会丢失它。我使用GetSquare来检查剩余的图像方块,如果方形非常小,我会尝试将图像推回去
private double GetSquare()
{
Rect rect = Utils.GetBounds(Image, ImageCanvas);
var canvasRect = RootGeometry.Rect;
canvasRect.Intersect(rect);
return canvasRect.Height * canvasRect.Width;
}
double maxSquare=30000;
double allowedSquare=40000;
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
double square = GetSquare();
if (square < maxSquare || double.IsInfinity(square))
{
e.Complete();
int counter = 0;
while (GetSquare() <= allowedSquare)
{
counter++;
translateTransform.X -= 100 * e.Velocities.Linear.X;
translateTransform.Y -= 100 * e.Velocities.Linear.Y;
if (counter > 10)
{
ToInitState();
return;
}
}
}
else
{
translateTransform.X += e.Delta.Translation.X;
translateTransform.Y += e.Delta.Translation.Y;
}
//Rotate
rotateTransform.Angle += e.Delta.Rotation;
//Scale
double deltaScale = e.Delta.Scale;
double newScale = Utils.GetImageCurrentScale(Image) * deltaScale;
if (newScale < Utils.MAX_ZOOM && newScale > Utils.MIN_ZOOM)
{
scaleTransform.ScaleX *= deltaScale;
scaleTransform.ScaleY *= deltaScale;
}
}