问题与转换DOC为PNG [英] Issue with converting DOC to PNG

查看:310
本文介绍了问题与转换DOC为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。我已经.DOT我填充和转向.doc文件的文件。那么我想借此.doc文件,并把它变成一个图像。但问题是,图像是完美的在本地主机上(高品质),质量很差的现场服务器上。我的问题是,我怎么能救一个高品质的巴纽(或任何其他)的图像,而不是低质量的呢?因为同样的code工作在本地主机,但无法现场服务器上,它只是奇怪的。下面是我使用的转换方法:

 私人无效ConvertDocToPNG(字符串startupPath,字符串文件名1)
    {
        VAR docPath = Path.Combine(startupPath,文件名1);
        应用程序=新的应用程序();
        Microsoft.Office.Interop.Word.Document DOC =新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;        //打开Word文档,提取每个页面,并转换为图像
        的foreach(在doc.Windows Microsoft.Office.Interop.Word.Window窗口)
        {
            的foreach(在window.Panes Microsoft.Office.Interop.Word.Pane窗格)
            {
                对于(VAR I = 1; I< = pane.Pages.Count;我++)
                {
                    Microsoft.Office.Interop.Word.Page页= NULL;
                    布尔填充= FALSE;
                    而(!填充)
                    {
                        尝试
                        {
                            //这个!@#$变量并不总是准备好洒其网页了。如果步
                            //将code,它会一直工作。如果你只是执行它,它会崩溃。所以呢
                            //我做的是让code赶上一点通过让线程睡眠
                            //为微秒。第二次左右,这个变量应填入确定。
                            页= pane.Pages [I]
                            填充= TRUE;
                        }
                        赶上(前收到COMException)
                        {
                            Thread.sleep代码(1);
                        }
                    }
                    VAR位= page.EnhMetaFileBits;
                    VAR的目标= Path.Combine(startupPath +\\\\的String.Format({1} _page_ {0},我,filename1.Split()[0])'。');                    尝试
                    {
                        使用(VAR毫秒=新的MemoryStream((字节[])(位)))
                        {
                            VAR图像= System.Drawing.Image.FromStream(毫秒);
                            VAR pngTarget = Path.ChangeExtension(目标,PNG);
                            image.Save(pngTarget,ImageFormat.Png);
                        }
                    }
                    赶上(System.Exception的前)
                    {
                        doc.Close(真,Type.Missing,Type.Missing);
                        对Marshal.ReleaseComObject(DOC);
                        DOC = NULL;
                        app.Quit(真,Type.Missing,Type.Missing);
                        对Marshal.ReleaseComObject(应用);
                        应用= NULL;
                        扔恩;
                    }
                }
            }
        }
        doc.Close(真,Type.Missing,Type.Missing);
        对Marshal.ReleaseComObject(DOC);
        DOC = NULL;
        app.Quit(真,Type.Missing,Type.Missing);
        对Marshal.ReleaseComObject(应用);
        应用= NULL;
    }


解决方案

尝试明确设置的资源,

 形象画像= Image.FromStream(毫秒);
 位图MYBITMAP =新位图(的形象,新的大小(320480));
 myBitmap.Save(MyImage.png,System.Drawing.Imaging.ImageFormat.Png);

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

解决方案

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

这篇关于问题与转换DOC为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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