I'm trying to make use of an image pasted from the clipboard, like a screenshot.
Here is a snippet of code:
public void PasteImage(Image pasteImage)
{
MemoryStream image = new MemoryStream();
pasteImage.Save(image, pasteImage.RawFormat);
image.Position=0;
byte[] byteImage = image.ToArray();
On the line:
pasteImage.Save(image, pasteImage.RawFormat);
I get this error:
An unhandled exception of type 'System.ArgumentNullException' occurred in System.Drawing.dll
Additional information: Value cannot be null.
I'm following previous stackoverflow posts I've found on this topic, such as How to compare two images using byte arrays, and How to convert image in byte array, and just can't get it to work.
I'm wondering if there is something specific about images retrieved from the clipboard. I'm using this code to fetch the image:
if (Clipboard.ContainsImage())
{
Image pasteImage = GetClipboardImage();
PasteImage(pasteImage);
}
EDIT
This is the code behind GetClipboardImage():
public Image GetClipboardImage()
{
System.Drawing.Image returnImage = null;
if (Clipboard.ContainsImage())
{
returnImage = Clipboard.GetImage();
}
return returnImage;
}
If I put a breakpoint on the line that errors I can see that both pasteImage and pastImage.RawFormat are not null:
This looks relevant as an example Ill have a crack at that and report back: How can I get an image out of the clipboard without losing the alpha channel in .NET?