The example provided in Microsoft's documentation for Clipboard.ContainsImage()
includes the following:
System.Drawing.Image returnImage = null;
if (Clipboard.ContainsImage())
{
returnImage = Clipboard.GetImage();
....
}
The superficial / nominal behavior of this method is to first check if the clipboard contains an image, and if so acquire that image for use. Return null
otherwise.
However, isn't it possible that in-between the call to ContainsImage()
and the call to GetImage()
another application has changed the contents of the clipboard? There might be no image data after all.
When the clipboard does not contain an image, GetImage()
is documented to return null
. Fine, but then what is the point of calling ContainsImage()
in the first place, if when you call GetImage()
it is mandatory to inspect the result anyway?
This doesn't just apply to this example - what ever would be the use of calling ContainsImage()
if you actually need the clipboard contents?
Maybe ...
It is more performant than calling
GetImage()
, therefore its worth doing even though in a small % of casesGetImage()
will fail?Some magic locking is going on which solves this problem automatically (highly doubtful)?
A case where ContainsImage()
might be useful would be if you do not need to acquire the clipboard contents, just to see if they are imagery.