WPF:如何将<image.layouttransform>中的更改保存到文件?

时间:2017-01-30 18:56:13

标签: image image-processing

我正在使用MVVM模式编写WPF应用程序,这类似于Microsoft Paint clone。我在主窗口中显示图像并通过

进行转换
       <Image x:Name="img" HorizontalAlignment="Left" VerticalAlignment="Top" Width="{Binding Width}"
        Height="{Binding Height}" Stretch="None" Source="{Binding ImageSource}" MouseDown="img_MouseDown" MouseUp="img_MouseUp">
            <Image.LayoutTransform>
                <TransformGroup>
                    <RotateTransform  Angle="{Binding RotateAngle}"/>
                    <ScaleTransform  ScaleX="{Binding ScaleX}" ScaleY="{Binding ScaleY}"/>
                </TransformGroup>
            </Image.LayoutTransform>
        </Image>
    private void Save()
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.DefaultExt = "jpg";
        saveFileDialog.Filter = "JPG (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP (*.bmp)|*.bmp";
        saveFileDialog.AddExtension = true;
        if (saveFileDialog.ShowDialog() == true)
        {
            if (System.IO.Path.GetExtension(saveFileDialog.FileName) == ".jpg")
            {
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(ImageSource));
                using (var filestream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    encoder.Save(filestream);
                }
            }
            else if (System.IO.Path.GetExtension(saveFileDialog.FileName) == ".bmp")
            {
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(ImageSource));
                using (var filestream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    encoder.Save(filestream);
                }
            }
        }

    }

我知道我的所有转换都只存储在XALM文件中,所以我现在的保存功能不起作用。有可能获得有关Layouttransorm的信息并制作新的BitmapImage吗?

0 个答案:

没有答案