Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing WPF to Generate Images in a WCF service
    text
    copied!<p>I am generating WPF .png images in my WCF service. All works well for a while but eventually I get this error:</p> <pre><code> System.Windows.Markup.XamlParseException: Initialization of 'System.Windows.Controls.TextBlock' threw an exception. ---&gt; 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) </code></pre> <p>I have never been able to create this error locally, only on my hosts web server. I have tried several fixes and workarounds including:</p> <p><a href="https://stackoverflow.com/questions/8995588/asp-net-throws-win32exception-when-creating-bitmapimage-cannot-find-file-specif">ASP.NET Throws Win32Exception when creating BitmapImage, cannot find file specified</a> <a href="http://www.thejoyofcode.com/Generating_images_using_WPF_on_the_Server.aspx" rel="nofollow noreferrer">http://www.thejoyofcode.com/Generating_images_using_WPF_on_the_Server.aspx</a></p> <p>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:</p> <pre><code>// 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( () =&gt; { UserControl userControl = new UserControl() { DataContext = "Hello World" }; // Use PngBitmapEncoder to convert the control to a bitmap. stream = userControl.ToPng(173, 173); }); return stream; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload