-1

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;
}
Community
  • 1
  • 1
Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • 1
    I'm taking a guess, but I think it might have something to do with using a UserControl from the web server. I'm thinking that you can't instantiate a UserControl from a web application and that is why you are getting the error. The local test setup probably has permission to create UserControls but the web likely doesn't. – Kevin Green Mar 29 '12 at 18:15

1 Answers1

0

To anyone else having this issue. I was creating a new UserControl each time, setting its DataContext and converting it to PNG. Instead, just create the UserControl once at the beginning and keep hold of it and reuse it.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311