0

I'm trying to display an animated GIF on a form from an embedded resource, yet nothing is displayed, which makes me think loading from a stream doesn't work this way.

If I ditch this idea and load from file, the GIF is displayed correctly.

Is this something that just won't work, or have I made a mistake along the way? Also, this is being done from within a DLL.

My Code:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Set the image
    this.pictureBox.Image = GetImageFromManifest("TempNamespace.Wait.gif");

    // Remove the close button
    var hwnd = new WindowInteropHelper(this).Handle;
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}

public System.Drawing.Image GetImageFromManifest(string sPath)
{
    // Ready the return
    System.Drawing.Image oImage = null;

    try
    {
        // Get the assembly
        Assembly oAssembly = Assembly.GetAssembly(this.GetType());

        string[] names = oAssembly.GetManifestResourceNames();

        // Get the stream
        Stream oStream = oAssembly.GetManifestResourceStream(sPath);

        // Read from the stream
        oImage = System.Drawing.Image.FromStream(oStream);

    }
    catch (Exception ex)
    {
        // Missing image?           
    }

    //Return the image
    return oImage;
}

My XAML:

<wfi:WindowsFormsHost HorizontalAlignment="Center" VerticalAlignment="Center"  Height="50" Width="50">
    <winForms:PictureBox x:Name="pictureBox" Height="50" Width="50" SizeMode="StretchImage" BackgroundImageLayout="None"/>
</wfi:WindowsFormsHost>
kkrambo
  • 6,643
  • 1
  • 17
  • 30
Robbie C
  • 51
  • 2
  • 11
  • The stream loads fine, the exception is never called. – Robbie C Aug 21 '14 at 09:16
  • Maybe this could shed some light on it? http://stackoverflow.com/a/1388141/2484737 – Roel van Westerop Aug 21 '14 at 09:21
  • I gave that a go, too. Rather oddly, if I change the 'parent' window to be a fixed size (rather than resizing to the content of the parent form) the gif shows correctly. A little strange, but at least it can be fixed. – Robbie C Aug 21 '14 at 11:19
  • In fact, "this.SizeToContent = SizeToContent.WidthAndHeight;" is fine in the Window_Loaded method, but causes the gif to not load if called in the constructor. – Robbie C Aug 21 '14 at 11:24

1 Answers1

0

In summary, the problem was caused by setting the form's 'SizeToContent' value in either the XAML or the constructor (in my case, it was set to 'SizeToContent.WidthAndHeight').

Removing this property, and setting it in the 'Loaded' event rectified the issue. I assume that the Windows Form Host doesn't render correctly with animated GIF files when the form itself is not painted.

Robbie C
  • 51
  • 2
  • 11