我遇到了一个奇怪的问题。 我正在将图像应用于网格,它通过屏幕分辨率而不是逻辑地切入图像。 在 1280 x 800 下,它显示如下:
当它是 800 x 600 时,它显示如下:
这有点困惑,因为高度分辨率越低,它显示的图片就越多。 知道为什么会这样吗? 这是我的代码:
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/