当前位置:首页 > 随心手记 > 个人笔记 > 正文内容

【RichEditControl】如何提取图片

博主2020-02-21个人笔记1804
private void GetImagesFromWord(string strFileName)
        {
            using (RichEditDocumentServer srv = new RichEditDocumentServer())
            {
                srv.LoadDocument(strFileName);
                DevExpress.XtraRichEdit.API.Native.Document doc = srv.Document;
                DevExpress.XtraRichEdit.API.Native.ParagraphCollection parCol = doc.Paragraphs;
                DocumentImageCollection dic = doc.GetImages(doc.Range);
                if (dic.Count == 0)
                    return;
                //创建文件夹
                int index = strFileName.LastIndexOf('.');
                if (index == -1)
                    return;
                string strDir = strFileName.Substring(0, index);
                if(!Directory.Exists(strDir))
                    Directory.CreateDirectory(strDir);
 
                
                int i = 0;
                foreach (DocumentImage di in dic)
                {
                    i++;
                    string strNewFileName = strDir + "\\";
                    DevExpress.Office.Utils.OfficeImage officeImage = di.Image;
                    string strSuffix = i.ToString()+".";
                    switch (officeImage.RawFormat) { 
                        case DevExpress.Office.Utils.OfficeImageFormat.Bmp:
                            strSuffix += "bmp";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Gif:
                            strSuffix += "gif";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Jpeg:
                            strSuffix += "jpg";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Png:
                            strSuffix += "png";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Icon:
                            strSuffix += "ico";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Emf:
                            strSuffix += "emf";
                            break;
                        case DevExpress.Office.Utils.OfficeImageFormat.Wmf:
                            strSuffix += "wmf";
                            break;
                        
 
                        default:
                            strSuffix += "bmp";
                            break;
                    }
                    strNewFileName += strSuffix;
                    File.WriteAllBytes(strNewFileName, officeImage.GetImageBytes(officeImage.RawFormat));
                }
                 
            }
        }

开发工具:VS2015 及以上; Devexpress 15.1 及以上

个人比较喜欢Devexpress RichEditControl 组件:

image.png

打赏 支付宝打赏 微信打赏

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。