In a C# winform app. I’m writing a Clipboard log manager that logs text to a log file, (everytime a Ctrl+c/x is pressed the copied/cut text gets appended to the file)
I also did the same for images, that is, if you press “prtScreen”, the screen shot you took will also go to a folder.
I do that by using a timer, inside I have something which ‘looks’ like this:
if (Clipboard.ContainsImage())
{
if (IsClipboardUpdated())
{
LogData();
UpdateLastClipboardData();
}
}
This is how the rest of the methods look like:
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");
}
The problem is: inside the update method, “LastClipboardImage != Clipboard.GetImage()” is always returning true!
I even did the following inside the update method:
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.
Please help, the comparison isn’t working… why?
A little test. Call two times the GetImage method for the same image
it returns always false. So every time you call Clipboard.GetImage() you get a different image instance and thus you cannot compare then using a simple
== operatorYou are comparing two different instances of an Image object and of course they are not the same.
If you really want to compare the image down to the pixel level you need a more invasive (and performance hungry method) like this
Notice how this is possible because the Color structure defines an Equality operator that checks if the color RGB values are the same between two colors