Rotate a BitmapImage

10,073

Solution 1

How about this?

var transformBitmap = (TransformedBitmap)image1.Source;
RotateTransform rotateTransform = (RotateTransform)(transformBitmap.Transform);
rotateTransform.Angle += 90;
image1.Source = transformBitmap.Clone();

Solution 2

How about this:

BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(ImagePath);

// here
image.Rotation = Rotation.Rotate270; // or 90, 0, 180

image.EndInit();
Share:
10,073
Akrem
Author by

Akrem

Updated on June 06, 2022

Comments

  • Akrem
    Akrem almost 2 years

    I want to rotate a bitmap image I wrote some code and it work

    TransformedBitmap TempImage = new TransformedBitmap();
    
    TempImage.BeginInit();
    TempImage.Source = MyImageSource; // MyImageSource of type BitmapImage
    
    RotateTransform transform = new RotateTransform(90);
    TempImage.Transform = transform;
    TempImage.EndInit();
    
    image1.Source = TempImage;
    

    but I want that MyImageSource get this modification, because like that if I click again in the button nothing happen and this normal it get the first form of my image, and also I want it to take this form because I have to save it after modification.

    why I have to do this:

    I have some tiff image to read some of them can be not in the right form I want to add flip 90° the user click on it until the image return to the right form and when he click on flip the image will be saved(replaced) on disk in the actual form chosen by user

  • Akrem
    Akrem over 12 years
    I got exception : Unable to cast object of type 'System.Windows.Media.Imaging.BitmapImage' to type 'System.Windows.Media.Imaging.TransformedBitmap'. in the first line of your code
  • Howard
    Howard over 12 years
    In the code you attached, TempImage is type of TransformedBitmap. So I convert it to TransformedBitmap. If you have trouble converting to TransformedBitmap, please make sure the code is the same as you attached.