c# - 按屏幕分辨率切入图像

标签 c# wpf image grid screen-resolution

我遇到了一个奇怪的问题。 我正在将图像应用于网格,它通过屏幕分辨率而不是逻辑地切入图像。 在 1280 x 800 下,它显示如下: link to image

当它是 800 x 600 时,它显示如下: link to image

这有点困惑,因为高度分辨率越低,它显示的图片就越多。 知道为什么会这样吗? 这是我的代码:

Grid MyGrid = new Grid();
Image img = new Image();

StackPanel myStackPanel = new StackPanel();
myStackPanel.CanVerticallyScroll = true;

Image myImage = new Image();
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
myImageSource.UriSource = new Uri("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
myImageSource.EndInit();
myImage.Source = myImageSource;
// myImage.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
// myImage.VerticalAlignment = System.Windows.VerticalAlignment.Center;
// myStackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
// myStackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

myStackPanel.Children.Add(myImage);

MyGrid.Children.Add(myStackPanel);

_LayoutRoot.Children.Add(MyGrid);

最佳答案

问题:

myStackPanel.Children.Add(myImage);

不要使用 StackPanel如果你想适本地调整图像的大小(如果它什么都不包含,为什么还有一个 StackPanel 呢?),在这里它只会溢出和剪切。还有其他各种panels ,例如Grids,更适合这种情况。

关于c# - 按屏幕分辨率切入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241752/

相关文章:

wpf - DataTemplate 绑定(bind)中未调用 ValueConverter

java - 使用 JavaFX Scene Builder 在代码中更改 ImageView 图像

c# - Dapper ORM 的实用类类似于存储库模式

c# - 关于ServiceStack.Redis的一些问题

c# - 如何在WPF应用程序中保存图片

c# - 在组合框中的 XAML 中设置默认值

node.js - 类型错误 : Cannot read property 'image' of undefined

c# - 如何在 Windows 10 UWP 中复制和调整图像大小

c# - 在 System.Type 上使用条件断点时出错

C# 对象组合