/// <summary> /// 提取图片 /// </summary> private List <ImagesDetailInfo> GetPicsFromExcel() { List <ImagesDetailInfo> result = new List <ImagesDetailInfo>(); try { var workBook = Globals.ThisAddIn.Application.ActiveWorkbook; var workSheet = (Worksheet)workBook.ActiveSheet; FileOperateHelper.DeleteFolder(savePathGetImage); if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } for (int i = 1; i <= workSheet.Shapes.Count; i++) { var pic = workSheet.Shapes.Item(i); if (pic != null && pic.Type == Microsoft.Office.Core.MsoShapeType.msoPicture) { try { pic.Copy(); IDataObject ido = null; Dispatcher.Invoke(new System.Action(() => { try { ido = Clipboard.GetDataObject(); } catch { } })); if (ido != null && ido.GetDataPresent(DataFormats.Bitmap)) { System.Windows.Interop.InteropBitmap bmp = (System.Windows.Interop.InteropBitmap)ido.GetData(DataFormats.Bitmap); if (bmp != null) { if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } Util.SaveImageToFile(bmp.Clone(), savePathGetImage + pic.Name + ".jpg"); result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + pic.Name + ".jpg", UnCheckWordExcelRange = pic }); } } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } } } Dispatcher.Invoke(new System.Action(() => { System.Windows.Forms.Clipboard.Clear(); })); } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }
/// <summary> /// 提取图片 /// </summary> private List <ImagesDetailInfo> GetImagesFromWord() { List <ImagesDetailInfo> result = new List <ImagesDetailInfo>(); try { FileOperateHelper.DeleteFolder(savePathGetImage); if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } object xx = null; bool hasPic = false; int index = 1; foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in Application.ActiveDocument.Paragraphs) { foreach (InlineShape ils in paragraph.Range.InlineShapes) { if (ils != null) { if (ils.Type == WdInlineShapeType.wdInlineShapePicture) { //ils.Range.Copy(); //System.Drawing.Image image = null; //Dispatcher.Invoke(new Action(() => //{ // image = System.Windows.Forms.Clipboard.GetImage(); //})); //if (image != null) //{ // image.Save(savePathGetImage + "照片-" + index + ".jpg"); // result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + "照片-" + index + ".jpg", UnCheckWordRange = ils.Range }); // index++; //} //Dispatcher.Invoke(new Action(() => //{ // System.Windows.Forms.Clipboard.Clear(); //})); try { if (!hasPic) { Dispatcher.Invoke(new System.Action(() => { if (Clipboard.ContainsText()) { xx = Clipboard.GetText(); } })); } } catch (Exception ex) { } hasPic = true; try { ils.Select(); IDataObject ido = null; Dispatcher.Invoke(new Action(() => { try { Application.Selection.CopyAsPicture(); ido = Clipboard.GetDataObject(); } catch { } })); if (ido != null && ido.GetDataPresent(DataFormats.Bitmap)) { System.Windows.Interop.InteropBitmap bmp = (System.Windows.Interop.InteropBitmap)ido.GetData(DataFormats.Bitmap); if (bmp != null) { if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } Util.SaveImageToFile(bmp.Clone(), savePathGetImage + "照片-" + index + ".jpg"); result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + "照片-" + index + ".jpg", UnCheckWordRange = ils.Range }); index++; } } } catch { } } } } } if (hasPic) { Dispatcher.BeginInvoke(new Action(() => { System.Windows.Forms.Clipboard.Clear(); if (xx != null) { try { System.Windows.Forms.Clipboard.SetDataObject(xx); } catch (Exception ex) { } } })); } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }