C# 比较剪贴板类中的两个图像

C# 比较剪贴板类中的两个图像,c#,clipboard,clipboard-pictures,C#,Clipboard,Clipboard Pictures,在C#winform应用程序中。我正在编写一个剪贴板日志管理器,将文本记录到日志文件中(每次按下Ctrl+c/x时,复制/剪切的文本都会附加到文件中) 我也对图像做了同样的处理,也就是说,如果你按“prtScreen”,你拍摄的屏幕截图也会进入一个文件夹 我通过使用计时器来实现这一点,在我的内部我有一个“看起来”像这样的东西: if (Clipboard.ContainsImage()) { if (IsClipboardUpdated()) { LogData(); U

在C#winform应用程序中。我正在编写一个剪贴板日志管理器,将文本记录到日志文件中(每次按下Ctrl+c/x时,复制/剪切的文本都会附加到文件中) 我也对图像做了同样的处理,也就是说,如果你按“prtScreen”,你拍摄的屏幕截图也会进入一个文件夹

我通过使用计时器来实现这一点,在我的内部我有一个“看起来”像这样的东西:

if (Clipboard.ContainsImage())
{
  if (IsClipboardUpdated())
  {
    LogData();
    UpdateLastClipboardData();
  }
}
以下是其余方法的外观:

public void UpdateLastClipboardData()
{
  // ... other updates
  LastClipboardImage = Clipboard.GetImage();
}
// This is how I determine if there's a new image in the clipboard...
public bool IsClipboardUpdated()
{
  return (LastClipboardImage != Clipboard.GetImage());
}
public void LogData()
{
  Clipboard.GetImage().Save(ImagesLogFolder + "\\Image" + now_hours + "_" + now_mins + "_" + now_secs + ".jpg");  
}
问题是:在更新方法中,“LastClipboardImage!=Clipboard.GetImage()”总是返回true

我甚至在更新方法中执行了以下操作:

Image img1 = Clipboard.GetImage();
Image img2 = Clipboard.GetImage();
Image img3 = img2;
bool b1 = img1 == img2; // this returned false. WHY??
bool b2 = img3 == img2; // this returned true. Makes sense.

请帮忙,这个比较不起作用。。。为什么?

一点测试。对同一图像调用两次GetImage方法

void Main()
{
    Image bmp1 = Clipboard.GetImage();
    Image bmp2 = Clipboard.GetImage();

    if(bmp1 != null && bmp1 == bmp2)
        Console.WriteLine("True");
    else
        Console.WriteLine("False");
}
它总是返回false。因此,每次调用Clipboard.GetImage()时,都会得到不同的图像实例,因此无法使用简单的
==运算符进行比较

您正在比较图像对象的两个不同实例,当然它们并不相同

如果您真的想将图像与像素级进行比较,您需要一种更具侵入性的方法(并且需要更高的性能)

bool ImagesAreDifferent(Image img1, Image img2)
{
    Bitmap bmp1 = new Bitmap(img1);
    Bitmap bmp2 = new Bitmap(img2);

    bool different = false;
    if (bmp1.Width == bmp2.Width && bmp1.Height == bmp2.Height)
    {
        for (int i = 0; i < bmp1.Width; i++)
        {
            for (int j = 0; j < bmp1.Height; j++)
            {
                Color col1 = bmp1.GetPixel(i, j);
                Color col2 = bmp2.GetPixel(i, j);
                if (col1 != col2)
                {
                    i = bmp1.Width + 1;
                    different = true;
                    break;
                }
            }
        }
    }    
    return different;
}
bool图像差异(图像img1、图像img2)
{
位图bmp1=新位图(img1);
位图bmp2=新位图(img2);
bool-different=false;
if(bmp1.Width==bmp2.Width&&bmp1.Height==bmp2.Height)
{
对于(int i=0;i
请注意这是如何实现的,因为颜色结构定义了一个检查颜色RGB值在两种颜色之间是否相同的属性,该属性检查与的相等性,该属性测试引用类型的相等性,而不是语义相等性。这就是为什么
img2==img3
true
,正如您之前将
img2
的引用复制到
img3
中一样。但是,对于
img1
img2
,您调用了它来构造一个新的图像对象

如果您确实想要测试两个图像对象是否包含相同的数据,那么您需要编写自己的方法—如果您不想子类化
image
,那么可能需要编写一个扩展方法

public static class ImageExtensions
{
    public static bool MyEquals(this Image x, Image y)
    {
        // compute and return your definition of equality here
    }
}

请注意,
=
运算符不会自动调用此方法,您必须检查与
图像是否相等。MyEquals

我认为,您可以更改程序逻辑来克服此问题,而不是比较图像。 如何捕获添加到剪贴板的新项目的事件并写入日志

您可以从下面的链接尝试代码示例代码


解决了这个问题,谢谢!但是“性能饥渴”-->的确!我想知道是否有更好的方法。。。或者对这种方法进行调整,也许。。。
// Use the "ClipboardManager" to manage in a more comprehensive the clipboard
// I assume that "this" is a Form
ClipboardManager manager = new ClipboardManager(this);
// Use "All" to handle all kinds of objects from the clipboard
// otherwise use "Files", "Image" or "Text"
manager.Type = ClipboardManager.CheckType.All;
// Use events to manage the objects in the clipboard
manager.OnNewFilesFound += (sender, eventArg) => 
{
    foreach (String item in eventArg)
    {
        Console.WriteLine("New file found in clipboard : {0}", item);
    }
};
manager.OnNewImageFound += (sender, eventArg) =>
{
    Console.WriteLine("New image found in clipboard -> Width: {0} , Height: {1}", 
                      eventArg.Width, eventArg.Height);
};
manager.OnNewTextFound += (sender, eventArg) =>
{
    Console.WriteLine("New text found in clipboard : {0}", eventArg);
};
// Use the method "StartChecking" to start capturing objects in the clipboard
manager.StartChecking();
// Close the capturing
manager.Dispose();