【问题标题】:Issue with converting DOC to PNG将 DOC 转换为 PNG 的问题
【发布时间】:2014-07-18 16:43:35
【问题描述】:

我有一个奇怪的问题。我有 .dot 文件,我填充并转向 .doc 文件。然后我将这个 .doc 文件转换为图像。问题是,图像在本地主机上是完美的(高质量),而在实时服务器上质量很差。我的问题是,如何保存高质量的 .png(或任何其他)图像而不是低质量的图像?这很奇怪,因为相同的代码在本地主机上工作,但在实时服务器上失败。这是我使用的转换方法:

    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);

        doc.ShowGrammaticalErrors = false;
        doc.ShowRevisions = false;
        doc.ShowSpellingErrors = false;

        //doc.ActiveWindow.ActivePane.View.Zoom.Percentage = 500;

        //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");
                            image.Save(pngTarget, 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;
    }

【问题讨论】:

  • 服务器是否拥有所有必需的库?特别是那些用于 Microsoft.Office 的?
  • 确实如此。我也尝试使用缩放但无济于事。
  • 我注意到您将其标记为 ASP.NET 和 Office Interop。 Office Interop 在服务器上不受支持,您可能会遇到很多问题。见MSDN for details

标签: c# asp.net office-interop


【解决方案1】:

尝试明确设置 res,

 Image image = Image.FromStream(ms);
 Bitmap myBitmap = new Bitmap( image, new Size( 320,480 ) ); 
 myBitmap.Save( "MyImage.png", System.Drawing.Imaging.ImageFormat.Png ); 

【讨论】:

  • 这引发了“GDI+ 中发生一般错误”。错误。
  • 等等,我想我知道为什么了。默认路径可能被禁止到站点,所以我需要在目标中添加 Server.MapPath。让我再试一次。
  • 你在做某事......纵横比被吹到了比特,但图像分辨率确实增加了。这就是我所需要的,我会试着在剩下的问题上绞尽脑汁。非常感谢!
  • 欢迎您,这应该有助于保持纵横比stackoverflow.com/questions/10442269/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
相关资源
最近更新 更多