Results 1 to 2 of 2

Thread: [WPF]Window temporarily corrupted after OpenFileDialog to load large image

  1. #1
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,999

    [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:
    Name:  WPFWindowCorrupt.jpg
Views: 194
Size:  21.3 KB

    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

  2. #2
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,999

    [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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •