【问题标题】:Displaying animated GIF files from embedded resources显示来自嵌入资源的动画 GIF 文件
【发布时间】:2014-10-14 20:13:43
【问题描述】:

我正在尝试在来自嵌入式资源的表单上显示动画 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>

【问题讨论】:

  • 流加载正常,从不调用异常。
  • 也许这可以解释一下? stackoverflow.com/a/1388141/2484737
  • 我也试了一下。相当奇怪的是,如果我将“父”窗口更改为固定大小(而不是调整为父表单的内容),则 gif 会正确显示。有点奇怪,但至少可以修复。
  • 其实“this.SizeToContent = SizeToContent.WidthAndHeight;”在 Window_Loaded 方法中很好,但如果在构造函数中调用,则会导致 gif 不加载。

标签: c# wpf gif embedded-resource


【解决方案1】:

总的来说,问题是由在 XAML 或构造函数中设置表单的“SizeToContent”值引起的(在我的例子中,它被设置为“SizeToContent.WidthAndHeight”)。

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

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 2019-08-03
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2011-02-25
    • 2019-03-18
    相关资源
    最近更新 更多