1 Attachment(s)
[WPF]Window temporarily corrupted after OpenFileDialog to load large image
In the following code, I use an OpenFileDialog to select an image to place on Canvas1, which I added in the designer.
Code:
Class MainWindow
Private _Image As New Image
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim ofd As New Microsoft.Win32.OpenFileDialog
If ofd.ShowDialog = True Then
Canvas1.Children.Remove(_Image)
Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri(ofd.FileName)
bi.EndInit()
_Image.Source = bi
Canvas1.Children.Add(_Image)
'Me.InvalidateArrange() 'tried this, didn't help.
End If
End Sub
End Class
This works fine except for very large images -- usually somewhere over 32 MegaPixels, though that varies from run to run. When it goes wrong, the Window fails to render. Here's an example:
Attachment 85350
If I drag other windows over the corrupt window, they leave trails. But immediately I start to drag the WPF window itself by its title bar, it renders correctly. Can anyone suggest a way to deal with this in code?
BB
[WORKAROUND]Window temporarily corrupted after OpenFileDialog to load large image
I hunted the web for anything to do with forcing a WPF to render, including this promising looking Refresh method extension for UIElements; but nothing worked. Then I tried adding this at the end of the sub:
Code:
Me.Left+=1
Me.Left-=1
and it worked: the largest images I could find rendered correctly. (Of course I could also add and subtract 1 alternately in each call of the sub.) In fact, anything exceeding Me.Left+=0.4 or less than Me.Left-=0.6 seemed to work. And that suggests to me that it has something to do with rendering on a hardware pixel boundary.
My question stays open however. Can anyone suggest a "correct" way to deal with this problem? I should add I'm using WinXPh with 3GB memory; maybe the problem won't reproduce on other systems.
BB