增强图像处理(移动和缩放)性能

时间:2015-01-12 12:14:39

标签: c# wpf

我正在移动和缩放可变量(1到约80)的图像(110 x 110 px)simultanioulsy。每个显示的图像都是从磁盘加载到WPF Image控件的位图。使用触摸操作事件(ManipulationDelta)获取操纵的输入。

一旦我试图操纵更多然后只是几张图像(3或4),一切都开始变慢。我怎样才能提高解决方案的性能?

对于操作,我使用以下代码:

触摸输入:

private void TouchManipulationContainer_ManipulationDelta(object sender, 
                                  ManipulationDeltaEventArgs e)
{
    var scale = e.DeltaManipulation.Scale.X;
    panAllSelected(e.DeltaManipulation.Translation.X, 
                   e.DeltaManipulation.Translation.Y, scale);
}

private void panAllSelected(double x, double y, double scale)
{
    foreach (var child in ImageGrid.Children)
    {
        if (child.GetType() == typeof(ImageDisplayContainer))
        {
            var tds = (child as ImageDisplayContainer);
            if (tds.IsSelected) tds.TouchDisplayControl.MoveAndZoom(x, y, scale);
        }
    }
}   

图片处理:

public void MoveAndZoom(double DeltaX, double DeltaY, double DeltaZoom)
{
    position.X += DeltaX;
    position.Y += DeltaY;

    scale += (DeltaZoom - 1.0);

    MovableImage.SetValue(ImageCanvas.LeftProperty, position.X);
    MovableImage.SetValue(ImageCanvas.TopProperty, position.Y);

    var x = MovableImage.GetValue(ImageCanvas.LeftProperty);
    var y = MovableImage.GetValue(ImageCanvas.TopProperty);
    Console.WriteLine(x + "; " + y + "; " + scale);

    if (scale != 1.0)
    {
        var sizeX = ImageCanvas.ActualWidth * scale;
        var sizeY = ImageCanvas.ActualHeight * scale;
        var size = Math.Max(sizeX, sizeY);

        MovableImage.Width = Math.Abs(size);
        MovableImage.Height = Math.Abs(size);
    }
}

XAML:

<Grid ClipToBounds="True">
    <Canvas x:Name="ImageCanvas" Background="Black">
        <Image x:Name="MovableImage"
               Source="/ImageInspector3;component/Images/Data/Thumbnails/ImageViewer_Series1.png"/>
    </Canvas>
</Grid>

1 个答案:

答案 0 :(得分:1)

好吧,3/4个物体并且已经放慢速度,听起来非常糟糕。

克莱门斯的想法还不错。您可以尝试Canvas.Left而不是Canvas.TopMatrixTransform属性(我在处理触摸事件时已经看过很多次)。

 <Image x:Name="MovableImage" 
        Source="...myPicture"
        RenderTransform="0 0 0 0.0 0 0" />

您可以操纵Matrix

的方式
var element = (Image)args.Source;
MatrixTransform xform = element.RenderTransform as MatrixTransform;
Matrix matrix = xform.Matrix;

ManipulationDelta delta = args.DeltaManipulation;
Point center = args.ManipulationOrigin;

matrix.ScaleAt(delta.Scale.X, delta.Scale.Y, center.X, center.Y);
matrix.Translate(delta.Translation.X, delta.Translation.Y);
xform.Matrix = matrix;

更多链接:http://msdn.microsoft.com/en-us/magazine/ff898416.aspx

http://blog.andreweichacker.com/2010/04/wpf-4-0-multitouch-manipulation/