Saving a WinForms control’s image to a file, should be pretty straightforward. After all, there is a DrawToBitmap method that should do the trick, right? Well, not quite. Unfortunately DrawToBitmap draws the controls in reverse order, i.e. the top controls are drawn first. So first you need to reverse the z-order of the controls on the form and then reverse them again then after generating the image, something like the following
InvertZOrderOfControls(formControl); using (Bitmap bitmap = new Bitmap(formControl.Width, formControl.Height)) { formControl.DrawToBitmap(bitmap, new Rectangle(0, 0, formControl.Width, formControl.Height)); bitmap.Save(outputFile, ImageFormat.Jpeg); } InvertZOrderOfControls(formControl);
Ah, but what about the implementation of InvertZOrderOfControls? You can find that here.
Even after that frigging around, the saved image may not be perfect. The output produced is dependant on the underlying operating system. Vista and XP produce reasonably accurate output, but Server 2003 doesn’t look too good.
No comments:
Post a Comment