按屏幕分辨率剪切图像
我遇到了一个奇怪的问题。 我将图像应用到网格中,它通过屏幕分辨率而不是逻辑地切割图像。 在 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);
I'm encoutering a wierd issue.
I'm applying an image to a grid, and it cuts through the image by screen reolution, and not logiclly.
At 1280 x 800 it shows like this:
and when it's 800 x 600 it shows like this:
Which is kinda messed up because with lower height resolution, it shows more of the picture.
Any idea why the hell that's happening?
this is my code :
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题:
不要使用
StackPanel< /code>
如果你想要正确调整图像大小(如果它不包含任何其他内容,为什么还要有一个 StackPanel?),这里它只会溢出并剪切。还有各种其他面板,例如
网格
,更适合于此。The problem:
Don't use a
StackPanel
if you want the image resized properly (why is there a StackPanel anyway if it contains nothing else?), here it will just overflow and cut. There are various other panels, e.g.Grids
, which are better suited for this.该程序完全按照您的指示执行。您正在使用
StackPanel
,这意味着其中的所有项目都将被“堆叠”。默认情况下,StackPanel
的方向是垂直的,这意味着第二个控件放置在第一个控件的下方,第三个控件放置在第二个控件的下方,依此类推。并且所有这些控件都使用它们可用的所有宽度。在你的情况下,这意味着图像被拉伸到窗口的宽度。如果高度不合适,底部就会被切掉。或者,如果窗口的高度太大,您会在图像下方看到空白区域。
查看此问题的最简单方法是不要将窗口最大化并尝试更改窗口的大小。
如果您不希望出现这种行为,您可以使用不同的面板,例如 HB 建议的 Grid 。
The program does exactly what you tell it to. You're using
StackPanel
, which means all items in it will be “stacked”. By default, the orientation of theStackPanel
is vertical, which means the second control is placed below the first one, the third below the second etc. And all of them use all of the width available to them.In your case, it means the image is stretched to the width of the window. If its height doesn't fit, the bottom will be cut off. Alternatively, if the height of the window was too big, you would see empty space below the image.
The easiest way to see this is to not have the window maximized and try changing the size of the window.
If you don't want this behavior, you can use different panels, like
Grid
, as H.B. suggested.