No not as an expense. If you disposed of the property due to foreclosure the funds would be included as a part of reduction of the debt during the foreclosure. IRS says a foreclosure is still considered a sale, or in more technical terms, a "disposition" of property.
The basic formula for calculating capital gains is to subtract the basis or cost of the property from the sales price. The difference is how much profit a person made, or how much money was lost on the transaction.