C # Implementing word converting PNG pictures

Source: Internet
Author: User

Because of the project needs, after some great God's guidance and github,stackoverflow to find information, wrote a program such as this.

The main reason is that Word documents have special fonts, and special font processing uses enhmetafilebits, which is to get enhanced meta information for one page of content. The type belongs to Emfplusdual.

Here, special thanks to github,stackoverflow,csdn three websites, people who are able to share technology and who are willing to help others.

Although most of the code can be found on the Internet, but there are a little bit of their own ideas, but also a little contribution to the code industry.

Code:

Using system;using system.collections.generic;using system.linq;using system.text;using InteropWord = Microsoft.office.interop.word;using system.io;using system.runtime.interopservices;using System.Drawing.Imaging; Using Microsoft.office.interop.word;namespace consoleapplication1{class Program {static void Main (string[]            args) {Word2png w = new Word2png ();        W.savetoimages ("d:\\");        }} class Word2png {application WordApp = null;        Private interopword.document Document = null;           public void Savetoimages (string directory) {This.wordapp = new application ();                        This.document = This.wordApp.Documents.Open ("C:\\2.docx");            Interopword.windows Windows = this.document.Windows; int windowcount = Windows.            Count;                for (var i = 1; I <= windowcount; i++) {Interopword.window win = windows[i]; Interopword.view WindoWsview = win.                View;                Pages can is retrieved in print layout view.                Windowsview.type = InteropWord.WdViewType.wdPrintView; Interopword.panes Panes = win.                Panes; int panecount = panes.                Count;                    for (var j = 1; J <= Panecount; j + +) {Interopword.pane Pane = panes[j]; var pages = pane.                    Pages; var pagecount = pages.                    Count;                    for (var k = 1; k <= PageCount;)                        {Interopword.page p = null;                        try {p = pages[k];  } catch {//pages[k] Sometimes throws exception:                            ' System.Runtime.InteropServices.COMException:The requested member of the collection does not exist '. This was a workaround for this ISSUe.                        Continue                        } var bits = p.enhmetafilebits; var target =directory+ string.                        Format (@ "\{0}_image.doc", K); using (var ms = new MemoryStream (byte[]) (bits)) {var image = System.d Rawing.                            Image.fromstream (MS);                            var imagetarget = path.changeextension (target, "PNG"); Image.                        Save (Imagetarget, imageformat.png);                        } marshal.releasecomobject (P);                        p = null;                    k++;                    } marshal.releasecomobject (pages);                    pages = null;                    Marshal.ReleaseComObject (Windowsview);                    Windowsview = null;                    Marshal.ReleaseComObject (pane);                pane = NULL; } marshal.releasecOmobject (panes);                panes = null;                Marshal.ReleaseComObject (Win);            win = null;            } marshal.releasecomobject (Windows);        Windows = NULL; } public void Close () {if (this.document! = null) {(Interopword._doc ument) this.document).                Close ();                Marshal.ReleaseComObject (this.document);                            This.document = null; }        }    }}

Disclaimer: Reprint Please indicate the source, there is a problem contact: [Email protected]

C # Implementing word converting PNG pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.