The RotateTransform works with xaml
As you known, RotateTransform
is for rotate transform in uwp app XAML. A RotateTransform
is defined by an Angle that rotates an object through an arc around the point CenterX, CenterY. But a transform is typically used to fill the UIElement.RenderTransform
property, so if you load the image source to an ImageControl
, you can rotate the ImageControl
since it is a UIElement
. For example, if we have ImageControl
as follows:
<Image x:Name="PreviewImage" Height="400" Width="300" AutomationProperties.Name="Preview of the image" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center"/>
We can simply rotate it by angle
property by code as:
RotateTransform m_transform = new RotateTransform();
PreviewImage.RenderTransform = m_transform;
m_transform.Angle = 180;
If you need rotate an image file not a UIElement
, you may need to decode the image file as what you already did and then encode the file with setting the BitmapTransform.Rotation
property. Code as follows:
double m_scaleFactor;
private async void btnrotatefile_Click(object sender, RoutedEventArgs e)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("test.bmp");
using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite),
memStream = new InMemoryRandomAccessStream())
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
uint originalWidth = decoder.PixelWidth;
uint originalHeight = decoder.PixelHeight;
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
if (m_scaleFactor != 1.0)
{
encoder.BitmapTransform.ScaledWidth = (uint)(originalWidth * m_scaleFactor);
encoder.BitmapTransform.ScaledHeight = (uint)(originalHeight * m_scaleFactor);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
}
//Rotate 180
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
await encoder.FlushAsync();
memStream.Seek(0);
fileStream.Seek(0);
fileStream.Size = 0;
await RandomAccessStream.CopyAsync(memStream, fileStream);
}
}
More features about the image file rotation you can use other APIS under Windows.Graphics.Imaging
namespace. And the scenario 2 of SimpleImaging official sample provides a complete sample about image rotation you can reference.