问题
I have few images thats being displayed, now there are some images where the width of the image is greater than the height of the image.
Say: image.GetWidth() > image.GetHeight();
display the image in landscape mode,
else
display the image in portrait mode.
I have searched,and could not find any resource that will help me in this case.
Any help would be appreciated.
Please not that i am on WP8.
EDIT
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;
回答1:
Try this,First add composite transform to image
<Image Name="image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" RenderTransformOrigin="0.5,0.5">
<Image.RenderTransform>
<CompositeTransform x:Name="compositeTransform"/>
</Image.RenderTransform>
</Image>
then check Height width(hope you have height width) of image and set composite transform rotation as per height width. Use -90 degree or +90 degree as per your requirement.
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");
For Code Behind first add composite transform then set it to image
CompositeTransform transform = new CompositeTransform();
transform.CenterX = 0.5;
transform.CenterY = 0.5;
image.RenderTransform = transform;
Then check Height width(hope you have height width) of image and set composite transform rotation as per height width. Use -90 degree or +90 degree as per your requirement.
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");
回答2:
To get the width and height of the Image,
double height = image1.ActualHeight;
double width = image1.ActualWidth;
来源:https://stackoverflow.com/questions/22627245/change-the-orientation-based-on-image-width-and-height