【发布时间】: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