Hello team,
I am working on a new requirement for my project where I need to save a group box as an image.
This groupbox has a UltraPictureBox in it and the UltrapictureBox has an image. On the image I placed some Activity controls. After I do some processing I need save the group box or the UltrapictureBox along with the activity controls on it as an image. Attached are the images which can provide a better insight of what my requirement is talking about.
For some reason i am not able to see any Activity controls when I saved the group box control shown in image #2, Image # 3 is what is expected
I have the below code currently to achieve this but not helping out.
Bitmap bmp = new Bitmap(WaferMapultraGroupBox.ClientRectangle.Width, WaferMapultraGroupBox.ClientRectangle.Height); WaferMapultraGroupBox.DrawToBitmap(bmp, WaferMapultraGroupBox.ClientRectangle); using (Graphics g = Graphics.FromImage(bmp)) { g.DrawImage(bmp, 0, 0); }
bmp.Save(filename, ImageFormat.Png);
Thanks in advance
Hello Baba Goud Gadiga,
I have been investigating into this behavior you have described, and the current behavior you are seeing with the code you have provided is actually expected. The reason for this is that DrawToBitmap will only draw the control that you pass to it and not the child controls of that control. This is not something specific to Infragistics either – this will happen with built-in .NET Windows Forms controls as well.
In order to achieve your requirement, I would recommend that you either recursively loop through the UltraGroupBox.Controls collection of your UltraGroupBox and draw them to the same Graphics object, or if you know the exact controls that you wish to draw, pick those out instead and draw them to a bitmap and then to your Graphics object.
I am attaching a sample project that demonstrates how this can be done.
Please let me know if you have any other questions or concerns on this matter.
WindowsFormsApp91.zip
Hi Andrew,
Thank you for looking at my post and answering to it. Your code works fine, but the problem I have is, On the picture box I have the other controls placed on it. So I modified your example form and added couple of button controls on the Groupbox1 control as shown in the below image and tried to capture it. The result is what I am seeing in my current application :) The After Processing image has a groupbox1 which does not show the button controls on it.
Thanks,
Baba
A more comprehensive solution would be to change the code so that it sorts the controls within each container by ZOrder, so that controls with a higher ZOrder (those that are on top) are at the bottom of the list of controls for that list - and thus paint last.
Unfortunately, it doesn't seem to be easy to get the ZOrder of a control. I think it might be enough just to loop through the Controls collection backward. that seems to work. So just change the GetDescendantControls method like so:
private static void GetDescendantControls(Control control, ref List<Control> controls) { controls.Add(control); for (int i = control.Controls.Count-1; i>=0; i--) { Control childControl = control.Controls[i]; GetDescendantControls(childControl, ref controls); } }
Of course, it's probably also a good idea NOT to have overlapping controls, anyway, since that will cause you other issues in the future.
Hello Mike and Andrew,
thank you for answering my query. You guys were right, the controls were just above the picture box. So I had to programmatically toggle SendToBack or BringToFront the picture box to get it work.
Thank you
Just in case there is something unusual about UltraButton, I modified the sample with a Button and an UltraButton on the UltraGroupBox and the GroupBox and they both work fine for me. This code won't work well if there are controls overlapping, because I didn't do anything to handle the painting order based on the control's ZOrder. So I'm guessing that maybe Andrew is right and your controls are not actually contained inside the GroupBox correctly and are just "above" it or something.
Here's the updated sample. If you are still having trouble, please post your sample so we can see what's going on there.
7853.WindowsFormsApp91.zip
Hello Baba,
From your newest screenshots, I believe at least one of the following things is happening with your application.
1. The buttons aren’t actually in the PictureBox: This can be checked either in your Form’s Designer.cs file or as a programmatic test to see if your UltraButton controls exist within the PictureBox.Controls collection.
2. The PictureBox is on top of the UltraButtons: If the above is true, then also depending on the order of the controls on your Form, there is a possibility that the PictureBox may end up further down the list of the “controlsToDraw” collection in the Utils class in the sample project from my original response. If this happens, the PictureBox will be drawn on top of your buttons, which is likely what is happening in your case here.
You will want to either ensure that the buttons truly are in the Controls collection of the PictureBox (if you are truly planning to put them there), or that they do not appear further down the list of controls in the “controlsToDraw” collection in the CaptureControlImageWithChildren method in the Utils class of the sample project, as these controls will be drawn in order of appearance in that collection.