Issue with converting DOC to PNG

无人久伴 提交于 2019-12-13 17:05:16

问题


I have a strange issue. I have .dot file that I populate and turn to .doc file. I then take this .doc file and turn it into an image. Problem is, the image is perfect on localhost (high quality) and very poor quality on live server. My question is, how can I save a high quality .png (or any other) image instead of a low quality one? It's just bizarre because the same code works on localhost but fails on live server. Here is the conversion method I am using:

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

回答1:


Try setting the res explicitly,

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


来源:https://stackoverflow.com/questions/24830027/issue-with-converting-doc-to-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!