[英]Rotating BitmapImage in C# / XAML
I have 2 BitmapImages in a List: 我在列表中有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);
How do I rotate both bitmap images with the push of a button? 如何通过按下按钮来旋转两个位图图像?
I have tried the solution (shown below) from MSDN, but I'm getting "no definition for 'Source'". 我已经尝试过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;
}
Take a look the following segments of code. 看看以下代码段。
XAML: 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>
Code behind: 代码背后:
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;
}
dbvega has answered my question. dbvega已经回答了我的问题。 However, there is a cast exception and I thought I'd address it for anyone who is using my question to solve your error(s).
但是,有一个强制转换异常,我想我会为任何使用我的问题来解决你的错误的人解决这个问题。
The correct C# code for dbvega's example should look like this: 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;
}
Hats off to dbvega though - problem solved! 虽然帽子关闭到dbvega - 问题解决了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.