C# 根据图像宽度和高度更改方向
我有一些正在显示的图像,现在有一些图像的宽度大于图像的高度 说:C# 根据图像宽度和高度更改方向,c#,image,silverlight,windows-phone-8,windows-phone,C#,Image,Silverlight,Windows Phone 8,Windows Phone,我有一些正在显示的图像,现在有一些图像的宽度大于图像的高度 说:image.GetWidth()>image.GetHeight() 以横向模式显示图像, 其他的 以纵向模式显示图像。 我已经搜索过了,但在这种情况下找不到任何有助于我的资源 任何帮助都将不胜感激 请不要告诉我我在WP8上 编辑 Grid grid = new Grid(); grid.VerticalAlignment = VerticalAlignment.Center; grid.HorizontalAlignment =
image.GetWidth()>image.GetHeight()代码>
以横向模式显示图像,
其他的
以纵向模式显示图像。
我已经搜索过了,但在这种情况下找不到任何有助于我的资源
任何帮助都将不胜感激
请不要告诉我我在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;
要获取图像的宽度和高度
double height = image1.ActualHeight;
double width = image1.ActualWidth;
尝试此操作,首先向图像添加复合变换
<Image Name="image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<CompositeTransform x:Name="compositeTransform"/>
</Image.RenderTransform>
</Image>
对于代码隐藏,首先添加复合变换,然后将其设置为图像
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)
{
compositeTransform.Rotation = 0.0;
}
else
{
compositeTransform.Rotation = 90.00;
}
image.Source =(ImageSource) new ImageSourceConverter().ConvertFromString("2011-Chrysler-300-Model-09-1024x1280.jpg");
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");
你的问题是什么?是否要在屏幕上呈现图像之前测试图像是否为image.Width>image.Height
?@agarwaen是,然后根据它更改方向跟随此链接@Sajeetharan该链接向我们提供方向信息,但不是如何动态地获取图像的宽度和高度runtime@user2056563尝试此项请查看我的编辑,帮助我根据需要进行修改尝试此项,添加CompositeTransform transform=new CompositeTransform();transform.CenterX=0.5;transform.CenterY=0.5;image.RenderTransform=变换;现在只需替换if-else语句中的transform。完成。!!是的,在.cs
文件中,我有与您共享的代码,我得到了这个错误系统。NullReferenceException:Object reference未设置为对象的实例,我添加了这个var transform=img.RenderTransform作为CompositeTransform代码>和内部如果宽度>高度
变换。旋转=90;您忘记初始化复合变换。首先初始化复合变换,然后将其设置为image。