旋转图像盒中的图像

时间:2019-05-08 21:26:49

标签: c# wpf visual-studio data-binding

我在学习MVVM方面很重要

我有一个装满图像的图像框 一键加载新图像,一键旋转图像。 如何旋转图像?

我想打开一个新图像并在保存后使用一些按钮对其进行一些转换,但只是开始旋转

在打开的按钮中,我找到了一些更新UriImg的代码,并且运行良好。

我找不到如何将来源分配给UriImg

MainWindow.xaml

<Window.Resources>
        <local:Imgcls x:Key="imgcls" />
    </Window.Resources>

    <Grid>
        <DockPanel Height="28" VerticalAlignment="Top">

        </DockPanel>
        <DockPanel Height="360">
            <Grid Width="400" Margin="10">
                <StackPanel>
                    <Button Content="open" Height="50" Click="Btn_Open_Click"/>
                    <Button Content="rotate"  Height="50" Click="Btn_Rotate_Click"/>
                </StackPanel>
            </Grid>
            <Grid Width="360">
                <Border BorderBrush="Black" BorderThickness="1">
                    <Image x:Name="img_box" Margin="5,5,5,5" Stretch="Uniform"
                           Source="{Binding UriImg ,Source={StaticResource imgcls}}">
                        <!--<Image.LayoutTransform>
                            <RotateTransform x:Name="imageTransform"/>
                            <ScaleTransform x:Name="imgScale" />
                        </Image.LayoutTransform>-->
                    </Image>
                </Border>
            </Grid>
        </DockPanel>
        <DockPanel Height="28" VerticalAlignment="Bottom">

        </DockPanel>
    </Grid>

Imgcls.cs

public class Imgcls : INotifyPropertyChanged
    {
        public Imgcls()
        {
            UriImg = "img/pic.png";
        }

        private string uri_img;        
        public string UriImg
        {
            get {return uri_img;}
            set
            {
                uri_img = value;
                Notify("UriImg");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void Notify(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

Mainwindow.xaml.cs

public partial class MainWindow : Window
    {
        private double rotation = 0;
        Imgcls img = new Imgcls();
        private BitmapImage source = new BitmapImage(new Uri(???, UriKind.Absolute));


        public MainWindow()
        {
            InitializeComponent();

        }
        private void Btn_Open_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog
            {
                Title = "Select Picture",
                Filter = "All supported|*.bmp;*.jpg;*.jpeg;*.png|" +
            "Bitmap (*.bmp)|*.bmp|" +
            "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
            "Portable Network Graphic (*.png)|*.png",
            };

            dlg.ShowDialog();
            var BE = img_box.GetBindingExpression(Image.SourceProperty);
            (BE.ResolvedSource as Imgcls).UriImg = dlg.FileName;
            BE.UpdateTarget();
        }

        private void Btn_Rotate_Click(object sender, RoutedEventArgs e)
        {
            //imageTransform.Angle += 90;
            rotation += 90;
            img_box.Source = new TransformedBitmap(source, new RotateTransform(rotation));

        }


    }

我不明白如何编写代码才能使源代码在buton_click上旋转。

如果我使用笔直的位置作为源,那么图像框将被该图像填充,并且在已打开的图像上不会发生旋转

0 个答案:

没有答案