Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WPF中多次旋转图像_C#_Wpf_Image_Rotation - Fatal编程技术网

C# 如何在WPF中多次旋转图像

C# 如何在WPF中多次旋转图像,c#,wpf,image,rotation,C#,Wpf,Image,Rotation,我已经用“打开文件”对话框打开了一个图像 image.Source=新的位图图像(新的Uri(ofd.FileName)) 然后,我想旋转它很多次,因为我喜欢,最后保存修改后的图像。问题在于MSDN中的代码: var biOriginal = (BitmapImage)image.Source; var biRotated = new BitmapImage(); biRotated.BeginInit(); biRotated.UriSource = biOriginal.UriSource;

我已经用“打开文件”对话框打开了一个图像

image.Source=新的位图图像(新的Uri(ofd.FileName))

然后,我想旋转它很多次,因为我喜欢,最后保存修改后的图像。问题在于MSDN中的代码:

var biOriginal = (BitmapImage)image.Source;
var biRotated = new BitmapImage();
biRotated.BeginInit();
biRotated.UriSource = biOriginal.UriSource;
biRotated.Rotation = Rotation.Rotate90;
biRotated.EndInit();
image.Source = biRotated;

我可以旋转图像,但只能旋转一次,无法保存旋转后的图像。

如果我没有弄错,您只需旋转图像即可。可以通过对XAML中的
Image
元素应用布局变换,并在单击按钮时更改其(变换的)角度值来完成此操作。另外,看起来您没有关注MVVM。如果您这样做,请查看它有多简单:

查看

<Image Source="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" 
    HorizontalAlignment="Center" VerticalAlignment="Center" Width="125">
        <Image.LayoutTransform>
            <RotateTransform Angle="{Binding RotateAngle}" />
        </Image.LayoutTransform>
</Image>
<Button Content="Rotate" Command="{Binding RotateCommand}" 
    VerticalAlignment="Bottom" HorizontalAlignment="Center" />
查看隐藏代码

ViewModel vm;

public View()
{
    InitializeComponent();
    vm = new ViewModel();
    DataContext = vm;
}


我假设您不是MVVM/WPF的绝对初学者,并且省略了BaseViewModel(implements INotifyPropertyChanged)和RelayCommand(implements ICommand)的定义,因为我不想让答案太长。如果您在这些方面遇到问题,请告诉我,我将在此处包括它们。

以下代码似乎有效:

            BitmapImage newImage = new BitmapImage();
        newImage.BeginInit();
        newImage.UriSource = MyImage.UriSource;
        newImage.Rotation = Rotation.Rotate90;
        newImage.EndInit();
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        string ImageName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), (Guid.NewGuid() + ".jpg"));
        encoder.Frames.Add(BitmapFrame.Create(newImage));

        using (var filestream = new FileStream(ImageName, FileMode.Create))
            encoder.Save(filestream);
        MyImage = new BitmapImage(new Uri(ImageName));

每次旋转图像时,它都会创建一个新图像当然,如果您想旋转图像不止一次,并且只保存一次,我还没有找到一种简单的方法来进行多次旋转而不保存旋转后的图像。

和(如果您想进行多次旋转,请在循环中重复代码)由于异常,无法将类型为“System.Windows.Media.Imaging.BitmapImage”的对象强制转换为类型为“System.Windows.Media.Imaging.TransformedBitmap”,因此无法进行那样的旋转。
            BitmapImage newImage = new BitmapImage();
        newImage.BeginInit();
        newImage.UriSource = MyImage.UriSource;
        newImage.Rotation = Rotation.Rotate90;
        newImage.EndInit();
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        string ImageName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), (Guid.NewGuid() + ".jpg"));
        encoder.Frames.Add(BitmapFrame.Create(newImage));

        using (var filestream = new FileStream(ImageName, FileMode.Create))
            encoder.Save(filestream);
        MyImage = new BitmapImage(new Uri(ImageName));