PopUp中的图像方向

时间:2014-05-13 07:33:37

标签: c# image silverlight windows-phone-8 windows-phone

我正在使用Windows Phone 8应用程序。

我有一张正在PopUp中显示的图片。但我有一些标准,如。

if image width > image height - display in landscape mode其他display in portrait mode

那么我们怎样才能实现这一目标呢?我搜索过这个,发现Popup没有定向属性。

double actualHeight = Application.Current.Host.Content.ActualHeight;
double actualWidth = Application.Current.Host.Content.ActualWidth;

 Grid grid = new Grid();
            grid.VerticalAlignment = VerticalAlignment.Center;
            grid.HorizontalAlignment = HorizontalAlignment.Center;
            grid.Height = actualHeight; //set height
            grid.Width = actualWidth; //set width
            grid.Background = new SolidColorBrush(Colors.White);

            Image img = new Image();
            img.VerticalAlignment = VerticalAlignment.Center;
            img.HorizontalAlignment = HorizontalAlignment.Center;
            img.Source = bitmapImage;
            img.Tap += OnFullScreenImageTap;

//below i am passing the calculated imageWidth and imageHeight

            if (imageWidth > imageHeight)
            {
                //Landscape
                Debug.WriteLine("Display image in LANDSCAPE");
            }
            else if (imageWidth == imageHeight)
            {
                //Portrait
                Debug.WriteLine("Display image in PORTRAIT");
            }
            else
            {
                //Portrait
                Debug.WriteLine("Display image in PORTRAIT");
            }
            grid.Children.Add(img);

            popUp.Child = grid; //set child content
            this.LayoutRoot.Children.Add(popUp);
            popUp.IsOpen = true;

修改

这也是我试过的:

RotateTransform  rt = new RotateTransform();
            rt.CenterX = 20;
            rt.CenterY = 20;

            img.VerticalAlignment = VerticalAlignment.Center;
            img.HorizontalAlignment = HorizontalAlignment.Center;
            img.Source = bitmapImage;

            if (imageWidth > imageHeight)
            {
                //Landscape

                rt.Angle = 0;
                Debug.WriteLine("Display image in LANDSCAPE");
            }
            else if (imageWidth == imageHeight)
            {
                //Portrait

                rt.Angle = 90;
                Debug.WriteLine("Display image in PORTRAIT");
            }
            else
            {
                //Portrait
                rt.Angle = 90;
                Debug.WriteLine("Display image in PORTRAIT");
            }
            img.RenderTransform = rt;

1 个答案:

答案 0 :(得分:1)

试一试

        double actualHeight = Application.Current.Host.Content.ActualHeight;
        double actualWidth = Application.Current.Host.Content.ActualWidth;

        Grid grid = new Grid();
        grid.VerticalAlignment = VerticalAlignment.Center;
        grid.HorizontalAlignment = HorizontalAlignment.Center;
        grid.Height = actualHeight; //set height
        grid.Width = actualWidth; //set width
        grid.Background = new SolidColorBrush(Colors.White);

        Image img = new Image();
        img.VerticalAlignment = VerticalAlignment.Center;
        img.HorizontalAlignment = HorizontalAlignment.Center;
        img.Source = bitmapImage;
        img.Tap += OnFullScreenImageTap;

        RotateTransform rt = new RotateTransform
        rt.CenterX = actualWidth / 2;
        rt.CenterY = actualHeight / 2;

        if (imageWidth > imageHeight)
        {
            //Landscape
            rt.Angle = 0;
            Debug.WriteLine("Display image in LANDSCAPE");
        }
        else if (imageWidth == imageHeight)
        {
            //Portrait
            rt.Angle = 90;
            Debug.WriteLine("Display image in PORTRAIT");
        }
        else
        {
            //Portrait
            rt.Angle = 90;
            Debug.WriteLine("Display image in PORTRAIT");
        }
        grid.Children.Add(img);

        popUp.Child = grid; //set child content
        this.LayoutRoot.Children.Add(popUp);
        popUp.IsOpen = true;