This is a great question that many clients I have ask when considering filing for bankruptcy in Portland or Vancouver.
The truth of the matter is that it depends, but oftentimes, people fear bankruptcy because they think it will be the worst of "two evils." It can often be the case that bankruptcy is easier on your credit score than a foreclosure.
To be 100% truthful, whether you opt for bankruptcy or foreclosure, there will be a negative impact on your credit. That said, foreclosure does damage without providing any positive results, namely, helping to restore your credit as a Chapter 7 or Chapter 13 bankruptcy will. In addition, a foreclosure does not wipe debt you owe to creditors and typically has a longer lasting negative impact on your credit score. As if that weren't enough, having a foreclosure on a property on your credit history will make it a great deal harder for you to purchase a new home in the future.
When you file for bankruptcy it will start your credit history from the ground up and allow you (in most instances) to have a fresh start for your financial health. In some cases, people who file for bankruptcy can bounce back within 24 months or so, as they make a strong effort to stay on top of bills and apply for any credit lines they can -- ensuring, of course, that they make their payments on time, even if it's with a secured credit card.
Regardless of what the general "rules" are, you need to consult with a bankruptcy attorney to know whether bankruptcy is the right choice for your financial future.