Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to rotate a bitmap image I wrote some code and it work

C#
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.
Posted
Updated 13-Nov-17 23:55pm
v2
Comments
Sergey Alexandrovich Kryukov 5-Sep-11 12:45pm    
What do you want to achieve?
--SA
akremb22 5-Sep-11 13:15pm    
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 from chosen by user

Hi,

Your problem is that each time your rotate your image you are using you image store it's rotation as it's image rotation is set to 90 degrees it will not rotate any more it is rather simple to work around. Simply declare an int to store your rotation angle set it to 0 and add 90 degrees each time you call your function.


C#
int Rotate_angle = 0;

private void Button_Click(object sender, RoutedEventArgs e)
{
        TransformedBitmap TempImage = new TransformedBitmap();
 
        TempImage.BeginInit();
        TempImage.Source = MyImageSource; // MyImageSource of type BitmapImage

        RotateTransform transform = new RotateTransform(Rotate_angle+=90);
        TempImage.Transform = transform;
        TempImage.EndInit();
 
        image1.Source = TempImage ;
}



A much simpler method if you have your Image displayed in an Image control named "img" like this

XML
<grid>
    <stackpanel>
        <Image Name="img" Source="01.jpg"/>


        <Button Click="Button_Click" Content="CLICK"/>
    </stackpanel>
</grid>


is to put this as your code behind;

C#
int Rotate_angle = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
        img.RenderTransformOrigin = new Point(0.5, 0.5);
        img.RenderTransform = new RotateTransform(Rotate_angle+=45);
}


This method is cleaner and allows you to rotate by different angles.

Hope this helps you
Cheers
Chris
 
Share this answer
 
v2
Comments
C_Johnson 6-Sep-11 3:36am    
Please excuse the <pre lang""></pre> tags if they are visible as they are meant to show code blocks however something must have gone wrong - Cheers Chris
BobJanova 6-Sep-11 4:50am    
I updated your solution to format correctly. You'd selected 'treat this post as plain text'.
C_Johnson 6-Sep-11 5:43am    
Thanks I tried to see if this was the problem but it didn't work when I selected and reselected it I presume it was just a browser issue, Many thanks for reformatting it correctly - Chris
 
Share this answer
 
Comments
akremb22 5-Sep-11 12:28pm    
this is for a Bitmap type and the RotateFlip don't exist in the bitmapimage
Sergey Alexandrovich Kryukov 5-Sep-11 12:46pm    
OP uses WPF. Read carefully.
--SA
[no name] 7-Sep-11 0:09am    
i just given an idea

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900