0
我在我的WCF服務中生成WPF .png圖像。一切運作良好一段時間,但最終我得到這個錯誤:使用WPF在WCF服務中生成圖像
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服務器中的用戶控件有關。我在想,你不能從Web應用程序中實例化UserControl,這就是爲什麼你會收到錯誤。本地測試設置可能具有創建UserControl的權限,但Web可能不會。 – 2012-03-29 18:15:56