当使用自定义的TextWriter OutputStream的不可 [英] OutputStream is not available when a custom TextWriter is used

查看:581
本文介绍了当使用自定义的TextWriter OutputStream的不可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的功能,PDF转换为png图片,它的投掷上的错误
这条线 - > stream.WriteTo(Response.OutputStream);有一些东西错了??

 保护无效CreatePngFromPdf()
        {            尝试
            {                串PDFLocation =的String.Format(@\\\\ XXXX \\ {0} \\ {1} \\ {2}的.pdf,YR,Loc.Substring(0,4),Loc.Substring(4,4));
                Utilities.WebPDF.PDF WebPDF =新DocuvaultMVC.Utilities.WebPDF.PDF();                WebPDF.Credentials =新的NetworkCredential(@某某,某某);
                字节[] =为png WebPDF.StreamPdfPageAsPngResize(PDFLocation,的PageNumber,612,792);                MemoryStream的毫秒=新的MemoryStream(PNG);
                MemoryStream的流=新的MemoryStream();
                INT newWidth = 612;
                INT newHeight = 792;
                为System.Drawing.Image newImg = System.Drawing.Image.FromStream(毫秒);                位图TEMP =新位图(newWidth,newHeight,newImg.PixelFormat);
                图形newImage = Graphics.FromImage(临时);
                newImage.DrawImage(newImg,0,0,newWidth,newHeight);
                newImg.Dispose();                temp.Save(流ImageFormat.Png);
                stream.WriteTo(Response.OutputStream);
                temp.Dispose();
                stream.Dispose();
            }
            赶上(异常前)
            {
                的Response.Write(ex.Message.ToString());
            }
        }


解决方案

这是不是您的问题直接相关的,但是别的事情做的时候,我得到了同样的异常,所以我想我把这个问题的答案在这里为后人。 ..

我得到这个例外只是当网页被渲染,而不是去到相关的控制器动作时。

大部分头刮随之而来,直到我意识到,我已经使用 Html.Action (运行的动作并发出HTML内嵌),而不是 Url.Action (生成的URL)。

this is my function which converts pdf to png image, it's throwing an error on this line--> stream.WriteTo(Response.OutputStream); Is there some thing wrong??

protected void CreatePngFromPdf() 
        {

            try
            {

                string PDFLocation = string.Format(@"\\XXXX\{0}\{1}\{2}.pdf", Yr, Loc.Substring(0, 4), Loc.Substring(4, 4));
                Utilities.WebPDF.PDF WebPDF = new DocuvaultMVC.Utilities.WebPDF.PDF();

                WebPDF.Credentials = new NetworkCredential(@"xyz", "xyz");
                byte[] png = WebPDF.StreamPdfPageAsPngResize(PDFLocation,PageNumber, 612, 792);

                MemoryStream ms = new MemoryStream(png);
                MemoryStream stream = new MemoryStream();
                int newWidth = 612;
                int newHeight = 792;
                System.Drawing.Image newImg = System.Drawing.Image.FromStream(ms);

                Bitmap temp = new Bitmap(newWidth, newHeight, newImg.PixelFormat);
                Graphics newImage = Graphics.FromImage(temp);
                newImage.DrawImage(newImg, 0, 0, newWidth, newHeight);
                newImg.Dispose();

                temp.Save(stream, ImageFormat.Png);
                stream.WriteTo(Response.OutputStream);
                temp.Dispose();
                stream.Dispose();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

解决方案

This is not directly relevant to your problem, but I got the same exception when doing something else, so I thought I'd put that answer here for posterity...

I got this exception merely when the homepage was rendering, rather than when going to the relevant controller action.

Much head-scratching ensues, until I realise that I've used Html.Action (which runs the action and emits the HTML inline) rather than Url.Action (which generates the URL).

这篇关于当使用自定义的TextWriter OutputStream的不可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆