I am generating WPF .png images in my WCF service. All works well for a while but eventually I get this error:
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)
I have never been able to create this error locally, only on my hosts web server. I have tried several fixes and workarounds including:
ASP.NET Throws Win32Exception when creating BitmapImage, cannot find file specified http://www.thejoyofcode.com/Generating_images_using_WPF_on_the_Server.aspx
However, I have yet to find a method which works. Can someone please post some code that does not throw this error or at least can work around it somehow. This is what my service does:
// 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;
}