3

I display a photo I took on one of my pages.

I capture the photo in Portrait mode and it works ok.

When I show the picture on my next view, it treats the photo like it was taken in Landscape.

So I need to rotate the picture/image by -90 to correct this.

Here is the relevant code of my .XAML:

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanelx" Grid.Row="1" Margin="0,0,0,0">
    </Grid>

And here is the methods where I load the photo and put it into the ContentPanel.:

void loadImage()
    {
        // The image will be read from isolated storage into the following byte array

        byte[] data;

        // Read the entire image in one go into a byte array

        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {

            // Open the file - error handling omitted for brevity

            // Note: If the image does not exist in isolated storage the following exception will be generated:

            // System.IO.IsolatedStorage.IsolatedStorageException was unhandled 

            // Message=Operation not permitted on IsolatedStorageFileStream 

            using (IsolatedStorageFileStream isfs = isf.OpenFile("0.jpg", FileMode.Open, FileAccess.Read))
            {

                // Allocate an array large enough for the entire file

                data = new byte[isfs.Length];



                // Read the entire file and then close it

                isfs.Read(data, 0, data.Length);

                isfs.Close();

            }
        }



        // Create memory stream and bitmap

        MemoryStream ms = new MemoryStream(data);

        BitmapImage bi = new BitmapImage();

        // Set bitmap source to memory stream

        bi.SetSource(ms);

        // Create an image UI element – Note: this could be declared in the XAML instead

        Image image = new Image();

        // Set size of image to bitmap size for this demonstration

        image.Height = bi.PixelHeight;

        image.Width = bi.PixelWidth;

        // Assign the bitmap image to the image’s source

        image.Source = bi;

        // Add the image to the grid in order to display the bit map

        ContentPanelx.Children.Add(image);
        
    }
}

I am thinking on a simple rotate on the image after I've loaded this. I can do this in iOS, but my C# is skills are worse than bad.

Can anybody advise on this?

peterh
  • 11,875
  • 18
  • 85
  • 108

2 Answers2

7

If the image is declared in xaml you can rotate it like this:

//XAML
    <Image.RenderTransform>     
    <RotateTransform Angle="90" /> 
      </Image.RenderTransform>

Same thing can be done thru c# also. If you always rotate the image , then doint it in xaml is the better optioin

//C#
((RotateTransform)image.RenderTransform).Angle = angle;

Please try this one:

RotateTransform rt = new RotateTransform();
            rt.Angle = 90;

            image.RenderTransform = rt;
TutuGeorge
  • 1,972
  • 2
  • 22
  • 42
  • What is angle? Would -90 work if i want to rotate by -90 degrees? –  Apr 26 '12 at 11:19
  • The ((RotateTransform)image.RenderTransform).Angle = 90; gives an invalid Cast exception. I guess the = 90; isnt the problem. –  Apr 26 '12 at 12:04
-1

You can create a RotateTransform object to use for the image's RenderTransform property. This will cause WPF to rotate the Image control when rendered.

If you want to rotate the image about it's center you will also need to set the rotation origin, as shown below:

RotateTransform rt = new RotateTransform();
rt.Angle = 90;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5, 0.5);
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
Anthony.
  • 643
  • 7
  • 14