The question seems to be asked already, however I cannot find a relevant answer.
I am loading a BMP image to memory in a UWP app, and I would like to rotate it by either 90, 180 or 270, but I just cannot find the way to do this.
The imgSource.rotate() does not seem to exist anymore The RotateTransform works with xaml ....
Could anyone add the missing code by a chance please?
public async Task LoadImage()
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("test.bmp");
using (var stream = await file.OpenAsync(FileAccessMode.Read))
{
var decoder = await BitmapDecoder.CreateAsync(stream);
bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
// Code to rotate image by 180 to be added
bitmap.CopyToBuffer(imgSource.PixelBuffer);
}
}
As you known,
RotateTransform
is for rotate transform in uwp app XAML. ARotateTransform
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 theUIElement.RenderTransform
property, so if you load the image source to anImageControl
, you can rotate theImageControl
since it is aUIElement
. For example, if we haveImageControl
as follows:We can simply rotate it by
angle
property by code as: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 theBitmapTransform.Rotation
property. Code as follows: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.