WPF将图片旋转缩放


最近在做一个照片墙的动画,在网上找了找图片的缩放和旋转,总结了一下写出这篇文章,希望对新手来说有些用处。

cs部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication40
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void saySomething(string imageURI)
{
Image image = new Image(); 
this.canvas.Children.Add(image); 
image.SetValue(Canvas.LeftProperty,(double)0);
image.SetValue(Canvas.TopProperty, (double)0);
image.Source = new BitmapImage(new Uri(imageURI,UriKind.RelativeOrAbsolute));
RotateTransform angle = new RotateTransform(); //旋转
ScaleTransform scale = new ScaleTransform(); //缩放
TransformGroup group = new TransformGroup();
group.Children.Add(scale);
group.Children.Add(angle);

image.RenderTransform = group;
image.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);//定义圆心位置
EasingFunctionBase easeFunction = new PowerEase()
{
EasingMode = EasingMode.EaseInOut,
Power = 2
};
DoubleAnimation scaleAnimation = new DoubleAnimation()
{
From = 1, //起始值
To = 0.5, //结束值
EasingFunction = easeFunction, //缓动函数
Duration = new TimeSpan(0, 0, 0, 5, 0), //动画播放时间
};
DoubleAnimation angleAnimation = new DoubleAnimation()
{
From = 0, //起始值
To = 360, //结束值
EasingFunction = easeFunction, //缓动函数
Duration = new TimeSpan(0, 0, 0, 5, 0), //动画播放时间
};
scale.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation);
angle.BeginAnimation(RotateTransform.AngleProperty, angleAnimation);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
saySomething("/phono.png");
}
}
}

 

XAML代码:

<Window x:Class="WpfApplication40.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication40"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Canvas x:Name="canvas">
<Button Height="50" Width="70" Canvas.Left="13" Canvas.Top="261" Content="image" Click="Button_Click"/>
</Canvas>
</Grid>
</Window>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM