0

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:

enter image description here

enter image description here

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?

Community
  • 1
  • 1
SimonJGreen
  • 273
  • 1
  • 19
  • If you put a breakpoint on that line.. is `pasteImage` or `RawFormat` `null`? – Simon Whitehead Jun 26 '13 at 22:48
  • It has to be the calling code, I would guess that pasteImage is null. The only difference in the PasteImage snippet that I do is without the image.Position and I use GetBuffer() instead of ToArray() – Steve0 Jun 26 '13 at 22:48
  • I don't think images or files for that matter are directly copied to the clipboard. A reference to the original file location is copied and once you paste it does the copying. Copy an image to the clipboard then delete the image then try to paste. So in other words as far as I understand, you cannot get the bytes of an image from the clipboard because technically the clipboard doesn't contain an image. – The Muffin Man Jun 26 '13 at 22:50
  • since you are getting a NullReferenceException then probably the pastImage parameter is null, you can check it like this just to make sure: if (pasteImage == null) System.Diagnostics.Debug.WriteLine("pastImage is null"); then run the application in debug mode and check if you can see the "pastImage is null" message in the output window, if yes then i am correct. – Ibrahim Najjar Jun 26 '13 at 22:51
  • What is the code behind GetClipboardImage(); Seems to me that this should be Clipboard.GetImage(); – Steve0 Jun 26 '13 at 22:52
  • 2
    Also see this http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.getimage(v=vs.100).aspx and this http://stackoverflow.com/questions/998655/how-can-i-get-an-image-out-of-the-clipboard-without-losing-the-alpha-channel-in – The Muffin Man Jun 26 '13 at 22:53
  • I've updated my question in response to the above comments, thanks. – SimonJGreen Jun 27 '13 at 06:36

1 Answers1

0

Looking at the MSDN article for Save Image, you might want to try:

Instead of

pasteImage.Save(image, pasteImage.RawFormat);

try

pasteImage.Save(image, System.Drawing.Imaging.ImageFormat.RawFormat)
dotNetE
  • 616
  • 1
  • 6
  • 27