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