Convert Word to image (2)

Source: Internet
Author: User

The method mentioned in the previous article has major defects: (1) Sometimes the cables may be dropped, mainly horizontal cables; (2) some computers do not draw pictures.ProgramThe drawing method is not unique. Some are named untitled, and some are named Untitled.

After studying the entire day, I finally found the method. Today I only wrote this function.

Private bitmap copyfromwordtoimage () {bitmap B = NULL; object nothing = system. reflection. missing. value; object missing = system. reflection. missing. value; Microsoft. office. interOP. word. application wordapp = marshal. getactiveobject ("word. application ") as Microsoft. office. interOP. word. applicationclass; Microsoft. office. interOP. word. document worddoc = wordapp. activedocument; worddoc. activewindow. sele Ction. wholestory (); worddoc. activewindow. selection. copy (); intptr hwnd = clswindows. findwindow (null, worddoc. name + "-Microsoft Word"); try {If (clswindows. openclipboard (hwnd) {intptr DATA = clswindows. getclipboarddata (14); If (Data! = Intptr. zero) {using (Metafile mf = new Metafile (data, true) {int W = mf. width; int H = mf. height; int maxh = 1000; float r = (float) (W * 1.0/h); If (h> maxh) {H = maxh; W = (INT) (H * r);} int maxw = 600; If (W> maxw) {W = maxw; H = (INT) (W/R );} B = new Bitmap (W, H); graphics G = graphics. fromimage (B); G. clear (color. white); G. drawimage (MF, 0, 0); B. save (@ "D: \ WEB \" + system. guid. newguid () + ". JPEG ", system. drawing. imaging. imageformat. JPEG) ;}}} finally {clswindows. closeclipboard ();} return B ;}

 

The clswindows class is defined to encapsulate API functions.

Public class clswindows {[dllimport ("user32.dll")] [Return: financialas (unmanagedtype. bool)] public static extern bool openclipboard (intptr hwndnewowner); [dllimport ("user32.dll")] [Return: financialas (unmanagedtype. bool)] public static extern bool closeclipboard (); [dllimport ("user32.dll")] public static extern intptr getclipboarddata (system. uint32 uformat); [dllimport ("user32.dll", entrypoint = "findwindow", setlasterror = true)] public static extern intptr findwindow (string lpclassname, string lpwindowname ); [dllimport ("user32.dll", entrypoint = "find1_wex", setlasterror = true)] public static extern intptr find1_wex (intptr hwndparent, uint kernel, string lpszclass, string lpszwindow ); [dllimport ("user32.dll", entrypoint = "sendmessage", setlasterror = true, charset = charset. auto)] public static extern int sendmessage (intptr hwnd, uint wmsg, int wparam, int lparam); [dllimport ("user32.dll", entrypoint = "setforegroundwindow", setlasterror = true)] private Static extern void setforegroundwindow (intptr hwnd); [dllimport ("user32.dll", entrypoint = "getmenu")] public static extern int getmenu (INT hwnd ); [dllimport ("user32.dll", entrypoint = "getsubmenu")] public static extern int getsubmenu (INT hmenu, int NPOs); [dllimport ("user32.dll ", entrypoint = "getmenuitemid")] public static extern int getmenuitemid (INT hmenu, int NPOs); [dllimport ("user32.dll", entrypoint = "postmessage")] public static extern int postmessage (INT hwnd, int wmsg, int wparam, int lparam); Public const int wm_command = 0x111 ;}

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.