将DOC转换为PNG时的模糊图像

时间:2014-07-25 15:10:58

标签: c# asp.net office-interop

好的,我有一个问题,让我再次感到困惑。我有一些代码将DOC文件转换为PNG文件。当我在本地主机上执行此操作时,图像很好。当我使用相同的代码并将其放在实时服务器上时,图像非常小(与我收到DOC文件的DOT文件大小相同,基本上DOT被填写并变成DOC)。现在......这是疯狂的部分。如果我以管理员身份登录托管服务器,然后转到实时网站,即使我从iPhone上访问该网站,图像仍然很大且很清晰。一旦我退出托管服务器并刷新实时页面,图像就会再次变小。这是我用来将DOC转换为PNG的代码。另外,如果我使用方法2,我可以使图像更大,分辨率更高,但字体不合适。

    private void ConvertDocToPNG(string startupPath, string filename1)
    {
        var docPath = Path.Combine(startupPath, filename1);
        Application app = new Application();
        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
        app.Visible = false;
        doc = app.Documents.Open(docPath);
        app.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMaximize;
        app.ActiveWindow.ActivePane.View.Zoom.Percentage = 100;
        doc.ShowGrammaticalErrors = false;
        doc.ShowRevisions = false;
        doc.ShowSpellingErrors = false;

        //Opens the word document and fetch each page and converts to image
        foreach (Microsoft.Office.Interop.Word.Window window in doc.Windows)
        {
            foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
            {
                for (var i = 1; i <= pane.Pages.Count; i++)
                {
                    Microsoft.Office.Interop.Word.Page page = null;
                    bool populated = false;
                    while (!populated)
                    {
                        try
                        {
                            // This !@#$ variable won't always be ready to spill its pages. If you step through
                            // the code, it will always work.  If you just execute it, it will crash.  So what
                            // I am doing is letting the code catch up a little by letting the thread sleep
                            // for a microsecond.  The second time around, this variable should populate ok.
                            page = pane.Pages[i];
                            populated = true;
                        }
                        catch (COMException ex)
                        {
                            Thread.Sleep(1);
                        }
                    }
                    var bits = page.EnhMetaFileBits;
                    var target = Path.Combine(startupPath + "\\", string.Format("{1}_page_{0}", i, filename1.Split('.')[0]));

                    try
                    {
                        using (var ms = new MemoryStream((byte[])(bits)))
                        {
                            var image = System.Drawing.Image.FromStream(ms);
                            var pngTarget = Path.ChangeExtension(target, "png");

                            // Method 2
                            image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);

                            // Another way to save it using custom size
                            //float width = Convert.ToInt32(hfIdCardMaxWidth.Value);
                            //float height = Convert.ToInt32(hfIdCardMaxHeight.Value);
                            //float scale = Math.Min(width / image.Width, height / image.Height);
                            //int resizedWidth = (int)Math.Round(image.Width * scale);
                            //int resizedHeight = (int)Math.Round(image.Height * scale);
                            //Bitmap myBitmap = new Bitmap(image, new Size(resizedWidth, resizedHeight));
                            //myBitmap.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
                        }
                    }
                    catch (System.Exception ex)
                    {
                        doc.Close(true, Type.Missing, Type.Missing);
                        Marshal.ReleaseComObject(doc);
                        doc = null;
                        app.Quit(true, Type.Missing, Type.Missing);
                        Marshal.ReleaseComObject(app);
                        app = null;
                        throw ex;
                    }
                }
            }
        }
        doc.Close(true, Type.Missing, Type.Missing);
        Marshal.ReleaseComObject(doc);
        doc = null;
        app.Quit(true, Type.Missing, Type.Missing);
        Marshal.ReleaseComObject(app);
        app = null;
    }

2 个答案:

答案 0 :(得分:1)

鉴于您以无人值守的方式使用互操作,可能会发生各种奇怪/意外的事情。我承认,我不知道为什么你会遇到在不同环境中给出测试用例的症状。我有一种非常强烈的感觉,无人看管是罪魁祸首。互操作在用户的登录环境中运行,如果没有用户......那么......是的。那么,如何解决这个问题仍然无人看管?首先想到的是使用OpenXML SDK。这是以无人值守的方式操作Office文档的安全方式。我将它用于无人值守的报告生成。

假设:

  • 标准DOCX格式
  • 该文档包含文字/图片/样式/其他。它不只是一大堆图像(如果是,有更简单的方法来完成你需要的东西)

API:

http://www.microsoft.com/en-us/download/details.aspx?id=30425

但是,您无法使用OpenXML将文档转换为图像!我想到了一个解决方法,但是没有经过测试。我们的想法是将doc转换为html,然后渲染出html并将其填充到图像中。

以下是使用OpenXML将word doc转换为HTML的方法:

可以做各种便利事情的大型电动工具: http://powertools.codeplex.com/

您需要的具体模块:http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2014/01/30/transform-docx-to-html-css-with-high-fidelity-using-powertools-for-open-xml.aspx

这是一个方便的库,用于呈现HTML并将其转储到图像中: http://htmlrenderer.codeplex.com/

答案 1 :(得分:0)

using (var ms = new MemoryStream((byte[])(bits)))
        {
            var emf = new Metafile(ms);
            var scale = 400 / emf.HorizontalResolution;
            var Width= emf.Width * scale;
            var Height = emf.Height * scale;
            System.Drawing.Bitmap b = new System.Drawing.Bitmap((Int32)Width, (Int32)Height);
            var G = System.Drawing.Graphics.FromImage(b);
            G.Clear(System.Drawing.Color.White);
            G.DrawImage(emf, 0, 0, (float)Width, (float)Height);
            b.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
        }
相关问题