I'm updating the Source of an Image control based on a ComboBox selection.
我正在根据ComboBox选择更新Image控件的Source。
After the Image Source is updated, I need to read the ActualWidth and ActualHeight of the Image
更新图像源后,我需要读取图像的ActualWidth和ActualHeight
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.
我在第一次使用Image控件的Loaded事件打开对话框时工作,但是每次更新Source时此事件都没有明显提升。
Is there any way to get the Actual Size of the images loaded into the control after each Source update?
有没有办法在每次Source更新后获得加载到控件中的图像的实际大小?
1 个解决方案
#1
0
You can try to use the image sourceupdated event, but i do not always have any luck using this.
您可以尝试使用图像sourceupdated事件,但我并不总是有运气使用它。
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.
然后你可以编写ImageDownloadCompleted的代码来获得图像的实际高度和宽度。
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
#1
0
You can try to use the image sourceupdated event, but i do not always have any luck using this.
您可以尝试使用图像sourceupdated事件,但我并不总是有运气使用它。
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.
然后你可以编写ImageDownloadCompleted的代码来获得图像的实际高度和宽度。
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