滚动时平滑图像剪辑

时间:2012-02-16 21:37:45

标签: c# windows-phone-7 scroll image-clipping

因此,当您使用PhotoChooserTask时,我正在尝试实现像本机裁剪器一样的效果。如果你不熟悉,背景中有一个图像,顶部有一个边框,当进出边框时,图像看起来会改变不透明度。以下代码工作正常,但它不是非常平滑(剪切的图像略微滞后于拖动运动)。有没有办法加快剪辑或改变事物,以便减少滞后?我有以下XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid x:Name="cropContainer" Width="375" MouseLeftButtonDown="cropContainer_MouseLeftButtonDown" MouseLeftButtonUp="cropContainer_MouseLeftButtonUp" MouseMove="cropContainer_MouseMove">
        <Grid.RenderTransform>
            <TranslateTransform x:Name="cropTransform" />
        </Grid.RenderTransform>
        <Image x:Name="fullImage" Source="{Binding Image}" Opacity=".5" />
        <Image x:Name="clippedImage" Source="{Binding Image}">
            <Image.Clip>
                <RectangleGeometry Rect="0,0,375,267" />
            </Image.Clip>
        </Image>
    </Grid>
</Grid>
<Border x:Name="cropBorder" Grid.Row="1" BorderThickness="1" BorderBrush="White" Opacity="1" Height="267.90571169537624660018132366274" Width="375">
</Border>

以下是滑动过程中跟踪和剪裁的代码:

private Point _cropBorderOffset, _original, _newPosition;
private bool _moving;
private double _maxUp, _maxDown;

void Page_Loaded(object sender, RoutedEventArgs e)
{
    var transform = cropBorder.TransformToVisual(Application.Current.RootVisual);
    _cropBorderOffset = transform.Transform(new Point(0, 0));

    _maxDown = ((fullImage.ActualHeight - cropBorder.ActualHeight) / 2);
    _maxUp = -_maxDown;

    var rect = (clippedImage.Clip as RectangleGeometry).Rect;
    (clippedImage.Clip as RectangleGeometry).Rect = new Rect(rect.X, (fullImage.ActualHeight / 2) - 134, rect.Width, rect.Height);
}

private void cropContainer_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    cropContainer.CaptureMouse();
    _original = e.GetPosition(cropBorder);
    _moving = true;
}

private void cropContainer_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    cropContainer.ReleaseMouseCapture();
    _moving = false;
}

private void cropContainer_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    _newPosition = e.GetPosition(cropBorder);

    if(_moving)
    {
        var deltaY = _newPosition.Y - _original.Y;

        var transform = fullImage.TransformToVisual(Application.Current.RootVisual);
        var fullImageOffset = transform.Transform(new Point(0, 0));

        var marginTop = deltaY;

        //Too far down
        if (marginTop > _maxDown)
        {
            marginTop = _maxDown;
        }
        //Too far up
        else if (marginTop < _maxUp)
        {
            marginTop = _maxUp;
        }

        cropTransform.Y = marginTop;

        var rect = (clippedImage.Clip as RectangleGeometry).Rect;
        (clippedImage.Clip as RectangleGeometry).Rect = new Rect(rect.X, _cropBorderOffset.Y - fullImageOffset.Y, rect.Width, rect.Height);
    }
}

1 个答案:

答案 0 :(得分:0)

不是将图像绘制两次,一次是剪裁而是一次没有,你是不是可以简单地绘制半透明的矩形来遮挡两侧并画出一个白色的中心轮廓?这应该会大大减少滞后。

一旦选择了位置以实际在内存中进行裁剪,您就可以使用裁剪矩形的左侧,顶部,宽度和高度。