What does the following warning in Code Contracts for Net 4.0 mean and how to fix it???
CodeContracts: requires unproven: (image.PixelFormat & PixelFormat.Indexed) == 0
I'm doing either: var bmp = new Bitmap(pSize.Width, pSize.Height, System.Drawing.Imaging.PixelFormat.Indexed)
or var g = Graphics.FromImage(this._otherBitmap)
As an aside: There are some questions on SO about how mature code contracts is and if you will use them and if they still exist, but they are 2009 to 2011. 2013 now... What do you think???
Thanks in advance
The issue is that Graphics.FromImage()
can't be used with an indexed bitmap, and the corresponding contract assembly (System.Drawing.Contracts.dll
) contains a precondition to enforce that. The static checker can't find anything in your code to prove the requirement is satisfied, so it gives you that warning.
You'll have to make sure that this._otherBitmap
is not created with the PixelFormat.Indexed
format. If you're absolutely sure it's not, you could add this line above the call to Graphics.FromImage()
:
Contract.Assume((this._otherBitmap.PixelFormat & PixelFormat.Indexed) == 0);
...but since the warning is telling you about an actual requirement of the FromImage()
method, it'll assert or throw an exception if you're wrong.