根据图像宽度和高度更改方向

时间:2014-03-25 06:47:39

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

我显示的图像很少,现在有些图像的图像宽度大于图像的高度。

说:image.GetWidth() > image.GetHeight();

display the image in landscape mode, else display the image in portrait mode.

我已经搜索过,在这种情况下找不到任何可以帮助我的资源。

任何帮助都将不胜感激。

请注意我不在WP8上。

修改

Grid grid = new Grid();
grid.VerticalAlignment = VerticalAlignment.Center;
grid.HorizontalAlignment = HorizontalAlignment.Center;
grid.Height = height; //set height
grid.Width = width; //set width
grid.Background = new SolidColorBrush(Colors.White);
Image img = new Image();
img.VerticalAlignment = VerticalAlignment.Center;
img.HorizontalAlignment = HorizontalAlignment.Center;
img.Source = source;

2 个答案:

答案 0 :(得分:1)

试试这个,首先将复合变换添加到图像

        <Image Name="image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <CompositeTransform x:Name="compositeTransform"/>
            </Image.RenderTransform>
        </Image>

然后检查图像的高度宽度(希望您有高度宽度)并根据高度宽度设置复合变换旋转。根据您的要求使用-90度或+90度。

        image.Height = 300;
        image.Width = 400;
        if (image.Height > image.Width)
        {
            compositeTransform.Rotation = 0.0;
        }
        else
        {
            compositeTransform.Rotation = 90.00;
        }
        image.Source =(ImageSource) new ImageSourceConverter().ConvertFromString("2011-Chrysler-300-Model-09-1024x1280.jpg");

对于Code Behind,首先添加复合变换,然后将其设置为图像

    CompositeTransform transform = new CompositeTransform();
    transform.CenterX = 0.5;
    transform.CenterY = 0.5;
    image.RenderTransform = transform;

然后检查图像的高度宽度(希望您有高度宽度)并根据高度宽度设置复合变换旋转。根据您的要求使用-90度或+90度。

        image.Height = 300;
        image.Width = 400;
        if (image.Height > image.Width)
        {
            transform.Rotation = 0.0;
        }
        else
        {
            transform.Rotation = 90.00;
        }
        image.Source =(ImageSource) new ImageSourceConverter().ConvertFromString("2011-Chrysler-300-Model-09-1024x1280.jpg");

答案 1 :(得分:0)

获取图像的宽度和高度,

double height = image1.ActualHeight;
double width = image1.ActualWidth;
相关问题