Net 4.0的代码合同中的以下警告是什么意思,以及如何修复它?
CodeContracts: requires unproven: (image.PixelFormat & PixelFormat.Indexed) == 0
我可以选择:var bmp = new Bitmap(pSize.Width, pSize.Height, System.Drawing.Imaging.PixelFormat.Indexed)或var g = Graphics.FromImage(this._otherBitmap)
顺便说一句:关于how mature code contracts is和if you will use them和 if they still exist,有一些问题,但它们是从2009年到2011年。2013年到现在...你觉得呢?
提前感谢
发布于 2013-05-23 23:24:40
问题是Graphics.FromImage()不能与索引的位图一起使用,而相应的协定程序集(System.Drawing.Contracts.dll)包含一个强制执行的前提条件。静态检查器在您的代码中找不到任何东西来证明需求得到了满足,所以它会给出警告。
您必须确保不使用PixelFormat.Indexed格式创建this._otherBitmap。如果您完全确定不是这样,那么可以在调用Graphics.FromImage()的上方添加下面这一行
Contract.Assume((this._otherBitmap.PixelFormat & PixelFormat.Indexed) == 0);...but由于警告是告诉您FromImage()方法的实际需求,因此如果您错了,它将断言或抛出异常。
https://stackoverflow.com/questions/16714730
复制相似问题