3

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
  • 5,033
  • 8
  • 37
  • 64

2 Answers2

3

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();
Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
2

How about this?

var transformBitmap = (TransformedBitmap)image1.Source;
RotateTransform rotateTransform = (RotateTransform)(transformBitmap.Transform);
rotateTransform.Angle += 90;
image1.Source = transformBitmap.Clone();
DIF
  • 2,470
  • 6
  • 35
  • 49
Howard
  • 3,638
  • 4
  • 32
  • 39
  • 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 – Akrem Sep 05 '11 at 16:12
  • 1
    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. – Howard Sep 06 '11 at 00:50