【问题标题】:Change the orientation based on Image width and height根据图像宽度和高度更改方向
【发布时间】:2015-02-04 04:05:38
【问题描述】:

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

说: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;

【问题讨论】:

  • 您的问题是什么?是否要测试image.Width > image.Height
  • @agarwaen 是的,在它被渲染到屏幕上之前,然后根据那个改变方向
  • @Sajeetharan 该链接为我们提供了有关方向的信息,但没有提供如何在运行时动态获取图像的宽度和高度
  • @user2056563 试试这个stackoverflow.com/questions/966696/…

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


【解决方案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");

对于后面的代码,首先添加复合变换,然后将其设置为图像

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

【讨论】:

  • 请看我的编辑,帮我按我的要求修改
  • 试试这个,添加 CompositeTransform transform = new CompositeTransform();变换.CenterX = 0.5;变换.CenterY = 0.5; image.RenderTransform = 变换;现在只需替换​​ if-else 语句中的转换。完成了。!!
  • 是的,在.cs 文件中,我有与您共享的代码
  • 我收到此错误 System.NullReferenceException: Object reference not set to an instance of an object ,我已添加此 var transform = img.RenderTransform as CompositeTransform; 和内部 if width &gt; height transform.Rotation = 90;
  • 您忘记初始化复合变换。首先初始化复合变换,然后将其设置为图像。
【解决方案2】:

要获取Image的宽高,

double height = image1.ActualHeight;
double width = image1.ActualWidth;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2013-09-11
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多