c# - 在 C#/XAML 中旋转位图图像

标签 c# wpf xaml

我的列表中有 2 个位图图像:

    BitmapImage img1 = new BitmapImage((new Uri("Images/image1.jpg", UriKind.Relative)));
    BitmapImage img1 = new BitmapImage((new Uri("Images/image2.jpg", UriKind.Relative)));

    List<BitmapImage> images = new List<BitmapImage>();

    images.Add(img1);
    images.Add(img2);

如何通过按按钮旋转两个位图图像?

我已经尝试了 MSDN 中的解决方案(如下所示),但我得到“‘源’没有定义”。

private void TurnLeft_Click(object sender, RoutedEventArgs e)
{
    //Create source
    BitmapImage bi = new BitmapImage();

    //BitmapImage properties must be in a BeginInit/EndInit block
    bi.BeginInit();
    bi.UriSource = new Uri("Images/image1.jpg", UriKind.Relative);

    //Set image rotation
    bi.Rotation = Rotation.Rotate270;
    bi.EndInit();

    //set BitmapImage "img2" from List<BitmapImage> from source
    img2.Source = bi;
}   

最佳答案

看一下以下代码段。

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Image x:Name="Image1" Stretch="Uniform" >
        <Image.Source>
            <BitmapImage UriSource="Images/logo.png"/>
        </Image.Source>
    </Image>

    <Button x:Name="TurnLeftButton" Content="TurnLeft"
            Click="TurnLeftButton_Click"
            Grid.Row="1"/>
</Grid>

隐藏代码:

private void TurnLeftButton_Click(object sender, RoutedEventArgs e)
{
    var biOriginal = (BitmapImage) Image1.Source;

    var biRotated = new BitmapImage();
    biRotated.BeginInit();
    biRotated.UriSource = biOriginal.UriSource;
    biRotated.Rotation = Rotation.Rotate270;
    biRotated.EndInit();

    Image1.Source = biRotated;
}

关于c# - 在 C#/XAML 中旋转位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28786780/

相关文章:

C#,foreach 值到 TextBox

WPF 绑定(bind)复选框列表,其中 IsChecked 绑定(bind)到列表的成员资格

c# - Windows 8 Metro 风格列表框

c# - 如何在.net nest中更新现有已过滤的Elasticsearch查询

c# 用于 asp.net web 界面的 windows 身份验证

wpf - 在 WPF 关闭时执行代码

android - WPF InkCanvas 是否有任何 Android 模拟?

c# - 图像拖放(WPF 窗口)

c# - 是什么让 Enum.HasFlag 这么慢?

c# - Server.CreateObject + 服务器上下文 + COM :-)