2

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;
    }
Lukas
  • 2,885
  • 2
  • 29
  • 31
  • Does the server have all the required libraries? Specifically the ones for Microsoft.Office? – lucuma Jul 18 '14 at 16:49
  • It sure does. I also tried using zoom with no avail. – Lukas Jul 18 '14 at 16:58
  • I noticed you tagged this as ASP.NET and also Office Interop. Office Interop is **not supported on servers** and you'll likely run into many issues with it. See [MSDN for details](http://support.microsoft.com/kb/257757). – mason Jul 18 '14 at 17:03

1 Answers1

4

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 ); 
fuzzybear
  • 2,325
  • 3
  • 23
  • 45
  • That threw a "A generic error occurred in GDI+." error. – Lukas Jul 18 '14 at 16:57
  • @Lukas where is the error what line ? http://msdn.microsoft.com/en-us/library/0wh0045z(v=vs.110).aspx – fuzzybear Jul 18 '14 at 16:58
  • Wait I think I know why. The default path may be forbidden to the site, so I need to add Server.MapPath in the destination. Let me try again. – Lukas Jul 18 '14 at 17:01
  • You are onto something... the aspect ratio is blown to bits but the image resolution is indeed increased. That was what I needed, I'll try to wreck my brain around the rest of the problem. Thank you very much! – Lukas Jul 18 '14 at 17:04
  • Your welcome, this should help with preserving aspect ratio http://stackoverflow.com/questions/10442269/scaling-a-system-drawing-bitmap-to-a-given-size-while-maintaining-aspect-ratio – fuzzybear Jul 18 '14 at 17:06