【发布时间】:2012-03-29 13:25:43
【问题描述】:
我正在我的 WCF 服务中生成 WPF .png 图像。一段时间内一切正常,但最终我得到了这个错误:
System.Windows.Markup.XamlParseException: Initialization of 'System.Windows.Controls.TextBlock' threw an exception. --->
System.ComponentModel.Win32Exception: The system cannot find the file specified
at MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
at MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
at System.Windows.SystemResources.EnsureResourceChangeListener()
at System.Windows.SystemResources.FindResourceInternal(Object key, Boolean allowDeferredResourceReference, Boolean
mustReturnDeferredResourceReference)
at System.Windows.StyleHelper.GetThemeStyle(FrameworkElement fe, FrameworkContentElement fce)
at System.Windows.FrameworkElement.UpdateThemeStyleProperty()
at System.Windows.FrameworkElement.OnInitialized(EventArgs e)
at System.Windows.FrameworkElement.TryFireInitialized()
at System.Windows.FrameworkElement.EndInit()
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType
xamlType, Object obj, Boolean begin)
--- End of inner exception stack trace ---
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader
xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadComponent(Object component, Uri
resourceLocator)
我无法在本地创建此错误,只能在我的主机 Web 服务器上。我尝试了几种修复和解决方法,包括:
ASP.NET Throws Win32Exception when creating BitmapImage, cannot find file specified http://www.thejoyofcode.com/Generating_images_using_WPF_on_the_Server.aspx
但是,我还没有找到有效的方法。有人可以发布一些不会引发此错误的代码,或者至少可以以某种方式解决它。这就是我的服务所做的:
// Only one copy of the dispatcher (I have tried creating a new one each time too).
private BackgroundStaDispatcher dispatcher = new BackgroundStaDispatcher();
public Stream GetImage()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
Stream stream = null;
this.dispatcher.Invoke(
() =>
{
UserControl userControl = new UserControl()
{
DataContext = "Hello World"
};
// Use PngBitmapEncoder to convert the control to a bitmap.
stream = userControl.ToPng(173, 173);
});
return stream;
}
【问题讨论】:
-
我在猜测,但我认为这可能与使用 Web 服务器中的 UserControl 有关。我认为您无法从 Web 应用程序实例化 UserControl,这就是您收到错误的原因。本地测试设置可能具有创建 UserControls 的权限,但网络可能没有。