我打电话给一些使用.NET的BitmapData类的代码。我碰到了一些我无法在Google桌面上找到明确答案的地方。unlockbits,lockbits,并试试finally
因为它似乎LockBits和UnlockBits必须始终以一对叫,我使用的是这样的:
System.Drawing.Imaging.BitmapData tempImageData = tempImage.LockBits(
new System.Drawing.Rectangle(0, 0, tempImage.Width, tempImage.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, tempImage.PixelFormat);
try
{
//use external library on the data
}//Exception not handled here; throw to calling method
finally
{
tempImage.UnlockBits(tempImageData);
}
(我最近一直与using语句,这是非常有用的玩耍C#,这让我觉得我应该这样做。)麻烦的是,即使MS自己的文档(http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.unlockbits.aspx)也不认为它适合使用try-finally模式。
试试最后是必要的还是无偿的?
更新:我可能最终会捕捉并重新抛出异常,因为我不知道它可能是什么,也没有早期捕捉它们。
谢谢!
一个很好的观点,在这里。 该代码不是我们的;它属于我们合作的专有库。 – Chris 2008-09-25 15:33:41