![](/img/trans.png)
[英]ASP.NET [Win32Exception (0x80004005): Access is denied] Error
[英]ASP.NET MVC - generating PDF raises Win32Exception (0x80004005)
我正在嘗試根據屏幕截圖生成一些PDF。 當我在本地啟動該方法時,它可以正常工作。 但是,當我將其部署在客戶端計算機上時,出現標題中提到的錯誤。 方法如下:
public ActionResult GeneratePdf()
{
using (MemoryStream ms = new MemoryStream())
{
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
var gfxScreenshot = Graphics.FromImage(bmpScreenshot);
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bmpScreenshot.Save(Server.MapPath("~/Content/Images/Pdf/myscreenshot.png"), ImageFormat.Png);
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
DateTime dtNow = DateTime.Now;
document.Add(new Paragraph(dtNow.ToString() + " - Impression de rendez-vous"));
System.Drawing.Image myImage = System.Drawing.Image.FromFile(Server.MapPath("~/Content/Images/Pdf/myscreenshot.png"));
iTextSharp.text.Image jpgImg = iTextSharp.text.Image.GetInstance(myImage, System.Drawing.Imaging.ImageFormat.Jpeg);
jpgImg.ScaleAbsolute(800, 500);
document.Add(jpgImg);
document.Close();
writer.Close();
Response.ContentType = "pdf/application";
Response.AddHeader("content-disposition", "attachment;filename =Impression_Qutenza.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}
return View();
}
知道發生了什么嗎?
編輯:堆棧跟蹤:
[Win32Exception (0x80004005): Descripteur non valide]
System.Drawing.Graphics.CopyFromScreen(Int32 sourceX, Int32 sourceY, Int32 destinationX, Int32 destinationY, Size blockRegionSize, CopyPixelOperation copyPixelOperation) +512
AstellasSchedulerV2.Controllers.HomeController.GeneratePdf() +232
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +182
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +99
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
您發布的代碼不會截取在Web瀏覽器中運行的網頁的屏幕快照,而是截取正在運行Web服務器的PC當前屏幕的截屏 。 在您的本地PC上“運行正常”有兩個原因:
1-您的開發PC上同時運行服務器和Web瀏覽器。
2-您用於開發的Web服務器未作為服務運行。
部署到Internet Information Server的Web應用程序將在IIS服務進程下運行,並且服務進程無權訪問服務器屏幕,這就是您在堆棧跟蹤中看到錯誤的原因。
可能的解決方案:
1-使用PDF處理庫使用與服務網頁相同的數據在服務器上從頭開始生成PDF文件。
要么
2-使用HTML到PDF轉換工具從服務器上本地請求的等效頁面生成PDF文件。 示例: Wkhtmltopdf或Amyuni WebkitPDF ( 注意:我為Amyuni Technologies工作 )
ASP.NET代碼在服務器端運行,當您調用.CopyFromScreen()方法時,它將嘗試訪問服務器上的屏幕。 並且不允許該代碼訪問瀏覽器的屏幕。 此安全限制是必須的,否則,病毒和特洛伊木馬程序無需用戶許可即可竊取信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.