0

This question may already have an answer here: What is a NullReferenceException and how do I fix it? 17 answers I'm having problems with Clipboard.ContainsImage () sometimes working and sometimes raise an error "Object reference not set to an instance of an object". In my application I'm trying to get an image from EXCEL document.

Microsoft.Office.Interop.Excel.Picture pic = (Microsoft.Office.Interop.Excel.Picture)ws.Pictures(1);
    object Eclairage = null;
    if (pic != null)
    {
        //This code will detect what the region span of the image was
        int startCol = (int)pic.TopLeftCell.Column;
        int startRow = (int)pic.TopLeftCell.Row;
        int endCol = (int)pic.BottomRightCell.Column;
        int endRow = (int)pic.BottomRightCell.Row;


    pic.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
    if (Clipboard.ContainsImage())
    {

        Image img = Clipboard.GetImage();

        var picture = new Bitmap(img);

        Color clr = picture.GetPixel(3, 3);

        if (clr.Name == "ff000000") Eclairage = "01";

    }
}

Using the debugger I simply can't find what is the problem as everything is performed exactly the same when it is working and when it is not.

the exception pointing exactly on

var picture = new Bitmap(img);

so the value of img = null I'm wondering how is that possible if Clipboard.ContainsImage is positive

FaithAlgo
  • 11
  • 5
  • 1) Could the picture on the clipboard be so large you can't allocate the required memory to copy it out? Be sure to dispose of all images as soon as you no longer need them. 2) It's possible some other process copied data into the clipboard between the calls to `ContainsImage()` and `GetImage()` so you should probably check for null anyway. – dbc Feb 23 '15 at 22:39
  • 1) I repeated this treatment into a foreach for 30 pictures and I already tried to dispose all images but always the same. 2) I don't understand can you give me examples? – FaithAlgo Feb 23 '15 at 22:55
  • Look at the solutions on this post. They seem to get at the clipboard in a slightly different way than you do. Different interfaces and whatnot. http://stackoverflow.com/questions/21759417/how-can-i-export-an-excel-worksheet-as-image Pay attention to the comments below the accepted solution. It is possible you can avoid the clipboard completely as it is a fickle thing that the user can interfere with. – user922020 Feb 23 '15 at 23:37
  • If the duplicate bug didn't help you, you need to be more specific than saying you read it and that it didn't help you. If you're getting an exception, you need to provide the full stack trace; the line of code you posted shouldn't be able to throw `NullReferenceException` at all. Even if `img` were `null`, I'd guess the `Bitmap` constructor would throw `ArgumentNullException` instead of `NullReferenceException`. – Peter Duniho Feb 24 '15 at 04:54
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Feb 24 '15 at 04:54

0 Answers0