WPF - 图像控制实际大小
我在 WPF 中获取图像控件的 ActualHeight
和 ActualWidth
时遇到问题。当用户选择图像文件时,我想根据图像控件的尺寸调整所选图像的大小。
我试图在窗口初始化时获取Image.ActualHeight
和Image.ActualWidth
,但我发现Image控件的两个属性都是“0”。
那么如何获取图像控件的尺寸呢。
I have a problem in getting ActualHeight
and ActualWidth
of image control in WPF. When user selects the image file, I want to resize the selected image based on the dimensions of the image control.
I tried to get the Image.ActualHeight
and Image.ActualWidth
when window initializes, but I found that both properties of Image control are '0'.
So how to get the dimensions of the image control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FrameworkElement.ActualHeight 的评论说房产发挥其真正价值之前可能会有一些滞后。
控件的最终大小由 FrameworkElement.Arrange 设置(-覆盖)。您可以重写该方法并仅调用基类实现。它的返回值将是图像的实际大小。
the remarks for FrameworkElement.ActualHeight say that there might be some lag before the property has its real value.
The final size of your control is set by FrameworkElement.Arrange(-Override). You could override the method and just call the base class implementation. Its return value will be the actual size of your Image.
在我的脑海中,我认为您应该订阅图像控件上的 Load 事件,在该事件触发之前,ActualHeight/Width 不会更新。
Off the top of my head, I think you should subscribe to the Load event on the image control, the
ActualHeight/Width
are not updated until that event fires.控件的 ActualSize 在“Measure”布局过程之后设置(“Arrange”布局过程设置其位置)。另外两个答案很有帮助;容器的“排列”布局过程仅在测量其子级之后发生,并且图像控件的加载处理程序应在其第一个布局过程完成后调用。
The control's ActualSize is set after the "Measure" layout pass (the "Arrange" layout pass sets its location). The other two answers are helpful; the "Arrange" layout pass of the container only happens after its children have been measured, and the load handler of your image control should be called after its first layout pass has completed.
我发现的最佳解决方案是等到图像加载后。
然后对于 ImageDownloadCompleted 我有以下内容:
我希望这对您有用。
The Best solution I have found is to wait until after the Image has loaded.
Then for ImageDownloadCompleted i have the following:
I hope this works for you.