Search code examples
c#windows-phone-7xaml

Rotating an Image in Windows Phone


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?


Solution

  • 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;