匯出Word圖片-ConsoleApp2-台大說文小篆 csharp

                  using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
// https://stackoverflow.com/questions/12287908/save-pictures-from-word-to-folder-c-sharp
namespace WordDocStats
{
    class Program
    {
        internal const string destpath = @"c:\篆書\";
        //internal const string destpath = "S:\\黃老師工作_守真\\!!!!!字源\\篆書\\";
        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        static void Main()
        {
            // Open a doc file
            //const string f = @"S:\@@@華語文工具及資料@@@\大徐本說文_主控文件\說文卷01-有誤註記(跑程式時發現的).docm";
            const string f = @"c:\篆書\說文卷01-有誤註記(跑程式時發現的).docm";

            var wordApplication = new Application();
            //var document = wordApplication.Documents.Open(@"C:\Users\Username\Documents\document.docx");
            var document = wordApplication.Documents.Open(f);
            document.ActiveWindow.Visible = true;
            
            // For each inline shape, export it to a file
            // By inspection you can see that the first inline shape have index 1 ( and not zero as one might expect )
            for (var i = 1; i <= wordApplication.ActiveDocument.InlineShapes.Count; i++)
            {
               
            
            // closure
            // http://confluence.jetbrains.net/display/ReSharper/Access+to+modified+closure
            var inlineShapeId = i;
                //SaveInlineShapeToFile(inlineShapeId, wordApplication);
            
            // parameterized thread start
            // https://stackoverflow.com/a/1195915/700926
            var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));

            // STA is needed in order to access the clipboard
            // https://stackoverflow.com/a/518724/700926
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            
        }
            Console.Beep(5000, 1000);
        // Close word
        wordApplication.Quit();
            Console.ReadLine();
        }

        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        protected static void SaveInlineShapeToFile(int inlineShapeId, Application wordApplication)
        {
            // Get the shape, select, and copy it to the clipboard
            var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];
            if (checkthePic(inlineShape) == true)
            {
                Range PRng = inlineShape.Range.Paragraphs[1].Range;
                inlineShape.Select();
                wordApplication.Selection.Copy();
                
                // Check data is in the clipboard
                if (Clipboard.GetDataObject() != null)
                {
                    var data = Clipboard.GetDataObject();

                    // Check if the data conforms to a bitmap format
                    if (data != null && data.GetDataPresent(DataFormats.Bitmap))
                    {
                        // Fetch the image and convert it to a Bitmap
                        var image = (Image)data.GetData(DataFormats.Bitmap, true);
                        var currentBitmap = new Bitmap(image);

                        // Save the bitmap to a file
                        //currentBitmap.Save(@"C:\Users\Username\Documents\" + String.Format("img_{0}.png", inlineShapeId));
                        //currentBitmap.Save(destpath + string.Format("{0}.png",inlineShapeId));
                        currentBitmap.Save(destpath + string.Format("{0}.png", gettheWord(PRng)));
                    }
            }
         }

            bool checkthePic(InlineShape inlineShpe)
            {
                Range isPRng = inlineShpe.Range.Paragraphs[1].Range;                

                if (inlineShpe.Range.Previous().Text == " " && inlineShpe.Range.Next().Text == "(" && 
                        inlineShpe.Range.End-isPRng.Characters[1].End==6)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            string gettheWord(Range pRng)
            {
                string w="";int i=0;
                do
                {
                    i++;
                    if  (pRng.Characters[i].Text== "(")
                    {
                        if (i > 20)
                        {
                            break;
                        }
                        i++; break;
                    }                    
                } while (true);
                do                    
                    {
                    w = w+pRng.Characters[i].Text ;                    
                    i++;
                    if (i>20)
                    {
                        break;
                    }
                    } while (pRng.Characters[i].Text != ")");
                return  w.Replace("/","/");
            }
        }
    }
}