Saving bitmap/"screenshot" of form not working when form is not shown
I am developing a windows forms application with multiple forms. I would like to print one of the forms as an image file.
I am using this example: click
I have a form with various labels, textboxes, and a button. When I run SaveAsBitmap
by pressing the button, the form and its elements is correctly saved as a bmp file (just as if you took a screenshot of it and saved as an image).
Now, I have another form which is not to be shown to the user. (It's a form with no form border style.)
I would like to do the bmp saving on this form instead of the other. But no matter where in the code of this form I put the SaveAsBitmap
method, it only saves the background开发者_JAVA百科 of the form (no items such as labels).
However, if I put this.ShowDialog()
somewhere to show the form, and then afterwards run the SaveAsBitmap
method, it works as it should.
So the main point here is the fact that it does not work correctly when the form is not shown.
When using the SaveAsBitmap
method, I write: SaveAsBitmap(this, "C:\\test.bmp");
Any help appreciated!
This works for me;
theForm.hide();
...
using (var bitmap = new Bitmap(theForm.Width, theForm.Height)) {
theForm.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
bitmap.Save(@"c:\null\ss.bmp");
}
精彩评论