繁体   English   中英

比较Word文件和文件夹中的图片?

[英]Compare picture in Word file and in a folder?

所以我正在使用Word.Interloop并且为了比较两个图片,我想我必须将当前图片(在word文件中)转换为位图图像,然后将其与桌面上的位图图像对象进行比较? 或者这可能是一种更简单的方法吗?

Word.InlineShape x;
x.isEqual( Picture from Desktop/ bitmapImage.Object);

我做了一个小样本,展示了如何实现这一目标。 主要思想是将您的图像从桌面表示为Bitmap实例,然后逐个像素地将其与文档中的Bitmap实例进行比较。 比较是通过首先将内嵌形状复制到剪贴板,然后将其转换为Bitmap ,然后将其与参考(从桌面)进行比较来完成的 - 首先按大小进行比较然后逐个像素。

该示例使用.NET 4.5,Microsoft Office对象库版本15.0和Microsoft Word对象库版本15.0实现为C#控制台应用程序。

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;

namespace WordDocStats
{
    class Program
    {
        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        static void Main()
        {
            // Open a doc file
            var wordApplication = new Application();
            var document = wordApplication.Documents.Open(@"C:\Users\Username\Documents\document.docx");

            // Load the image to compare against.
            var bitmapToCompareAgainst = new Bitmap(@"C:\Users\Username\Documents\image.png");

            // For each inline shape, do a comparison
            // 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; 

                // parameterized thread start
                // https://stackoverflow.com/a/1195915/700926
                var thread = new Thread(() => CompareInlineShapeAndBitmap(inlineShapeId, bitmapToCompareAgainst, wordApplication));

                // STA is needed in order to access the clipboard
                // https://stackoverflow.com/a/518724/700926
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }

            // Close word
            wordApplication.Quit();
            Console.ReadLine();
        }

        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        protected static void CompareInlineShapeAndBitmap(int inlineShapeId, Bitmap bitmapToCompareAgainst, Application wordApplication)
        {
            // Get the shape, select, and copy it to the clipboard
            var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];
            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);
                    var imagesAreEqual = true;

                    // Compare the images - first by size and then pixel by pixel
                    // Based on: http://www.c-sharpcorner.com/uploadfile/prathore/image-comparison-using-C-Sharp/
                    if(currentBitmap.Width == bitmapToCompareAgainst.Width && currentBitmap.Height == bitmapToCompareAgainst.Height)
                    {
                        for (var i = 0; i < currentBitmap.Width; i++)
                        {
                            if(!imagesAreEqual)
                                break;

                            for (var j = 0; j < currentBitmap.Height; j++)
                            {
                                if (currentBitmap.GetPixel(i, j).Equals(bitmapToCompareAgainst.GetPixel(i, j)))
                                    continue;

                                imagesAreEqual = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        imagesAreEqual = false;
                    }
                    Console.WriteLine("Inline shape #{0} is equal to the 'external' bitmap: {1}", inlineShapeId, imagesAreEqual);
                }
                else
                {
                    Console.WriteLine("Clipboard data is not in an image format");
                }
            }
            else
            {
                Console.WriteLine("Clipboard is empty");
            }
        }
    }
}

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM