比较Word文件和文件夹中的图片? [英] Compare picture in Word file and in a folder?
问题描述
所以我正在使用Word.Interloop,为了比较两个图片,我想我必须将当前图片(在Word文件中)转换为位图图像,然后将其与桌面上的位图图像对象进行比较? 或者,也许这是一种更简单的方法?
So I'm using Word.Interloop and in order to compare two pics, I guess I have to transform the current picture(in word file) to a bitmap image and then compare it with a bitmap image object from desktop? Or perhaps the is a simpler way to do so?
Word.InlineShape x;
x.isEqual( Picture from Desktop/ bitmapImage.Object);
推荐答案
我制作了一个小示例,说明如何实现此目的.主要思想是将桌面上的图像表示为Bitmap
实例,然后将其逐像素与文档中的Bitmap
实例进行比较.比较方法是先将嵌入式形状复制到剪贴板,然后将其转换为Bitmap
,然后将其与参考(从桌面)进行比较-首先按大小,然后按像素进行比较.
I have made a small sample showing how this can be accomplished. The main idea is to represent your image from your desktop as a Bitmap
instance and then compare it pixel by pixel to the Bitmap
instance in your document. The comparison is done by first copying an inline shape to the clipboard, then turning it into a Bitmap
, and then compare it with the reference (from the desktop) - first by size and then pixel by pixel.
该示例使用.NET 4.5,Microsoft Office对象库15.0版和Microsoft Word对象库15.0版作为C#控制台应用程序实现.
The sample is implemented as a C# console application using .NET 4.5, Microsoft Office Object Library version 15.0, and Microsoft Word Object Library version 15.0.
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");
}
}
}
}
参考文献:
- 以参数开头的线程: https://stackoverflow.com/a/1195915/700926
- 从C#中的单词中提取内嵌形状作为图像: https://stackoverflow.com/a/7937590/700926
- 在C#中比较图像: http://www.c-sharpcorner.com /uploadfile/prathore/image-comparison-using-C-Sharp/
- 有关如何在C#中从剪贴板中检索图像的详细信息: https://stackoverflow.com/a/998825/700926
- 有关如何从C#访问剪贴板的详细信息: https://stackoverflow.com/a/518724/700926
- Threadstart with params: https://stackoverflow.com/a/1195915/700926
- Extracting inline shapes as images from word in C#: https://stackoverflow.com/a/7937590/700926
- Comparing images in C#: http://www.c-sharpcorner.com/uploadfile/prathore/image-comparison-using-C-Sharp/
- Details on how to retrieve an image from the clipboard in C#: https://stackoverflow.com/a/998825/700926
- Details on how to access the clipboard from C#: https://stackoverflow.com/a/518724/700926
这篇关于比较Word文件和文件夹中的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!