The simplest way to avoid the file lock that GDI+ puts on the file is to make a deep copy of the bitmap with the Bitmap(Image) constructor. Like this:
private void SetWallpaperButton_Click(object sender, EventArgs e) {
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
using (var img = Image.FromFile(openFileDialog1.FileName)) {
if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
this.BackgroundImage = new Bitmap(img);
}
}
}
The using statement ensures that the file lock gets released. And the Dispose() call ensures that the old bitmap gets destroyed quickly, important because you are often skirting OOM with large bitmaps on a 32-bit operating system.