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
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.