AlIon
AlIon

Reputation: 367

Rotate bitmap by code C# UWP

How can I rotate bitmap 90 degrees clockwise? I'm using next code to read an image from storage:

public async void GetImage()
    {
        string filename = code + ".jpg";

        Windows.Storage.StorageFile sampleFile = 
            await Windows.Storage.KnownFolders.CameraRoll.GetFileAsync(filename);


        BitmapImage img = new BitmapImage();

        img = await LoadImage(sampleFile);

        imageMain.Source = img;

    }

        private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        return bitmapImage;
    }

And I want to get image rotated. bitmapImage.Rotate don't work at the UWP. What solution is?

Upvotes: 2

Views: 1552

Answers (1)

AVK
AVK

Reputation: 3923

This can be achieved through RotateTransform Class

In Code Behind

RotateTransform _rotateTransform = new RotateTransform()
{
    CenterX = imageMain.ActualWidth/2,
    CenterY = imageMain.ActualHeight/2,
    Angle = 90
};
imageMain.RenderTransform = _rotateTransform;

In XAML

<Image Stretch="Fill" Source="Assets/Windows.jpg" x:Name="imageMain" Width="500" Height="500">
    <Image.RenderTransform>
        <RotateTransform CenterX="250" CenterY="250" Angle="90"/>
    </Image.RenderTransform>
</Image>

** If you notice you can see that CenterX and CenterYare half the size of Actual Image.

Good Luck.

Upvotes: 4

Related Questions