WPF 图像:多次源更新后获取实际大小

发布于 2024-10-18 13:14:47 字数 183 浏览 0 评论 0原文

我正在根据组合框选择更新图像控件的源。

更新图像源后,我需要读取图像的 ActualWidth 和 ActualHeight

我在第一次使用 Image 控件的 Loaded 事件打开对话框时设法使其工作,但每次更新时该事件都不会明显引发源头。

有没有办法在每次源更新后获取加载到控件中的图像的实际大小?

I'm updating the Source of an Image control based on a ComboBox selection.

After the Image Source is updated, I need to read the ActualWidth and ActualHeight of the Image

I managed this to work the first time the dialog opens using the Loaded event of the Image control, but this event doesn't obviously raise every time i update the Source.

Is there any way to get the Actual Size of the images loaded into the control after each Source update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

樱娆 2024-10-25 13:14:47

您可以尝试使用图像源更新事件,但我并不总是有运气使用此事件。

根据您的源,更好的解决方案是在加载时添加一个处理程序。

你可以尝试这样的事情:

Dim src As BitmapImage = New BitmapImage()
src.BeginInit()
src.UriSource = tURI
src.CacheOption = BitmapCacheOption.OnLoad
src.EndInit()
imgImage.SetCurrentValue(Image.SourceProperty, src)
AddHandler src.DownloadCompleted, AddressOf ImageDownloadCompleted

然后你可以编写 ImageDownloadCompleted 的代码来获取图像的实际高度和宽度。

为了获得实际宽度,我使用源图像的宽度而不是图像控件,如下所示:

Sub ImageDownloadCompleted(sender As Object, e As System.EventArgs)
    Dim src As BitmapImage
    Dim dwidth as Double
    Dim dheight as Double

    src = DirectCast(sender, BitmapImage)
    dwidth = src.Width 
    dheight = src.Height
    RemoveHandler src.DownloadCompleted, AddressOf ImageDownloadCompleted
End Sub

You can try to use the image sourceupdated event, but i do not always have any luck using this.

A better solution, depending on your source is to add a handler for when it is loaded.

you can try something like this:

Dim src As BitmapImage = New BitmapImage()
src.BeginInit()
src.UriSource = tURI
src.CacheOption = BitmapCacheOption.OnLoad
src.EndInit()
imgImage.SetCurrentValue(Image.SourceProperty, src)
AddHandler src.DownloadCompleted, AddressOf ImageDownloadCompleted

then you can write the code for ImageDownloadCompleted to get the actual height and width of the image.

To get actual widths, I use the width of the source image and not the image control, as shown below:

Sub ImageDownloadCompleted(sender As Object, e As System.EventArgs)
    Dim src As BitmapImage
    Dim dwidth as Double
    Dim dheight as Double

    src = DirectCast(sender, BitmapImage)
    dwidth = src.Width 
    dheight = src.Height
    RemoveHandler src.DownloadCompleted, AddressOf ImageDownloadCompleted
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文