从嵌入式资源显示动画GIF文件

时间:2014-08-21 09:00:31

标签: c# wpf gif embedded-resource

我试图在嵌入式资源的表单上显示动画GIF,但没有显示任何内容,这让我觉得从流中加载并不会这样。

如果我抛弃这个想法并从文件加载,GIF会正确显示。

这件事是不是有效的,或者我一路上犯了错误?此外,这是在DLL中完成的。

我的代码:

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;
}

我的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>

1 个答案:

答案 0 :(得分:0)

总之,问题是由于设置表单“SizeToContent&#39; XAML或构造函数中的值(在我的例子中,它被设置为&#39; SizeToContent.WidthAndHeight&#39;)。

删除此属性,并在“已加载”中设置该属性。事件纠正了这个问题。我假设当窗体本身未绘制时,Windows窗体主机无法使用动画GIF文件正确呈现。

相关问题