按屏幕分辨率剪切图像

发布于 2025-01-05 08:04:56 字数 1123 浏览 3 评论 0原文

我遇到了一个奇怪的问题。 我将图像应用到网格中,它通过屏幕分辨率而不是逻辑地切割图像。 在 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);

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:
link to image

and when it's 800 x 600 it shows like this:
link to image

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2025-01-12 08:04:56

问题:

myStackPanel.Children.Add(myImage);

不要使用 StackPanel< /code>如果你想要正确调整图像大小(如果它不包含任何其他内容,为什么还要有一个 StackPanel?),这里它只会溢出并剪切。还有各种其他面板,例如网格 ,更适合于此。

The problem:

myStackPanel.Children.Add(myImage);

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.

往日情怀 2025-01-12 08:04:56

该程序完全按照您的指示执行。您正在使用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 the StackPanel 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文