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