It's because the StartupPath is not what you think it is. Testing this on my system causes the designer view of the form to show an error message in HTML, helpfully rendered as one very long line. Carefully scrolling through it reveals that it looking for the image C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\pic.jpg In a simplified test I place the image directly onto an otherwise empty user control. I thought that the correct thing to do would be to stop the designer attempting to load the image like this:
public partial class ImageControl : UserControl {
String path;
public ImageControl() {
InitializeComponent();
path = Application.StartupPath;
}
private void ImageControl_Load(object sender, EventArgs e) {
if (!DesignMode) {
BackgroundImage = Bitmap.FromFile(Path.Combine(path, "pic.jpg"));
}
}
}
Job done you might think, but no, as this allows the form to render properly with the image displayed. So what is going on? Actually I don't know, but the only reasonable explanation is that the designer loads the form more than once, in DesignMode where the path is incorrect and at least once more, not in DesignMode, where the path is correct.