我有一个 Windows 服务。并在服务启动期间调用以下函数。
static public void SaveXpsPageToJpeg(string xpsFileName, int[] pages){
using (XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read)){
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
DocumentPaginator paginator = docSeq.DocumentPaginator;
// You can get the total page count from docSeq.PageCount
foreach(int pageNum in pages){
using (DocumentPage docPage = paginator.GetPage(pageNum)){
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)docPage.Size.Width,
(int)docPage.Size.Height,
96, // WPF (Avalon) units are 96dpi based
96,
System.Windows.Media.PixelFormats.Default);
renderTarget.Render(docPage.Visual);
JpegBitmapEncoder encoder = new JpegBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (FileStream pageOutStream = new FileStream(xpsDoc + ".Page" + pageNum + ".jpg", FileMode.Create, FileAccess.Write)){
encoder.Save(pageOutStream);
pageOutStream.Close();
}
}
}
}
}
相同的代码可以在 WPF 应用程序中完美运行。但是当我在服务中调试它时,它会抛出一个异常
我还添加了异常和内部细节的屏幕截图
http://imageshack.us/photo/my-images/62/97453717.png/
http://imageshack.us/photo/my-images/850/95569721.jpg/
我还尝试在配置管理器中更改 X64、X86 和 AnyCPU
我正在使用 Visual Studio 2010、.Net Framework 4
有人可以在这里帮忙吗