在 Windows Phone 中旋转图像

本文关键字:旋转 图像 Phone Windows | 更新日期: 2023-09-27 18:32:14

我在我的一个页面上显示我拍摄的照片。

我在肖像模式下拍摄了照片,它工作正常。

当我在下一个视图中显示图片时,它会像在"风景"中拍摄的照片一样对待照片。

所以我需要将图片/图像旋转 -90 来纠正这个问题。

这是我的相关代码.XAML:

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

以下是我加载照片并将其放入内容面板的方法:

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);
        
    }
}

加载后,我正在考虑对图像进行简单的旋转。我可以在iOS中做到这一点,但我的C#技能比糟糕更糟糕。

有人可以就此提出建议吗?

在 Windows Phone 中旋转图像

如果图像是在 xaml 中声明的,则可以像这样旋转它:

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

同样的事情也可以通过 c# 完成。如果您总是旋转图像,那么在 xaml 中使用它是更好的选择

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

请尝试这个:

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

可以创建一个 RotateTransform 对象以用于图像的 RenderTransform 属性。这将导致 WPF 在呈现时旋转 Image 控件。

如果要围绕其中心旋转图像,则还需要设置旋转原点,如下所示:

RotateTransform rt = new RotateTransform();
rt.Angle = 90;
image.RenderTransform = rt;
image.RenderTransformOrigin = new Point(0.5, 0.5);