惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
宝玉的分享
宝玉的分享
美团技术团队
量子位
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
爱范儿
爱范儿
J
Java Code Geeks
博客园 - Franky
Last Week in AI
Last Week in AI
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
GbyAI
GbyAI
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog

博客园 - eyye的眼睛

又到清明节 sql中去除重复的项 戒烟吧 李开复:如何设计你的2015年度计划 网易邮箱已经忘记了客户 用程序集编写clr表值函数:把正则表达式引入数据库中 从天气网提取气象预报数据 sql 几点记录 sql 重复数据只保留一条 (转)android-sdk_r20.0.3-windows无法正常更新 RichTextBox的拖放功能DragDrop 合并两个rtf文件 杀掉叽哩瓜叽(jlguaji.exe)的两种方法 股票数据(转) 如何将格式文本转化为图片 SQL Server 的事务处理的两种格式 sql IN语句如何优化?(转) 向EXCEL批量导出数据(转) 禅语
word转化为图片(2)
eyye的眼睛 · 2012-05-05 · via 博客园 - eyye的眼睛

上一篇的方法有很大的缺陷:(1)有的时候会掉线,主要是横线;(2)有的电脑没有画图程序,画图的方式也不唯一,有的叫无标题,有的叫未命名。

研究了整整一天,终于找到了方法,今天就只写了这一个函数

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

其中ClsWindows类是定义来封装API函数的

 public class ClsWindows
    {
        [DllImport("User32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool OpenClipboard(IntPtr hWndNewOwner);
        [DllImport("User32.dll")]
        [return: MarshalAs(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 = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, 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;        
    }