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开发者_如何学JAVA Actual Size of the images loaded into the control after each Source update?
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
精彩评论