我在列表中有2个BitmapImages:
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;
}
答案 0 :(得分:2)
查看以下代码段。
<强> 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;
}
答案 1 :(得分:0)
dbvega回答了我的问题。但是,有一个演员例外,我想我会为使用我的问题来解决你的错误的任何人解决它。
dbvega示例的正确C#代码应如下所示:
private void TurnLeftButton_Click(object sender, RoutedEventArgs e)
{
var biRotated = new BitmapImage();
biRotated.BeginInit();
biRotated.UriSource = new Uri("Images/logo.png", UriKind.Relative);
biRotated.Rotation = Rotation.Rotate270;
biRotated.EndInit();
Image1.Source = biRotated;
}
虽然骗了dbvega - 问题解决了!