Hi,
I am trying to paint a Control to a Bitmap. I don't want to use DrawToBitmap, because it has a few Bugs (Z-Order, disappearing Elements, ...)
I am trying to using Reflection and OnPaint. That's the Method:
Bitmap ControlToBitmap(Control control) { Bitmap bitmap = new Bitmap(control.Width, control.Height); Graphics g2 = Graphics.FromImage(bitmap); Rectangle clipRect = new Rectangle(0,0,control.Width, control.Height); g2.Clip = new System.Drawing.Region(clipRect); PaintEventArgs pea = new PaintEventArgs(g2, clipRect); Type type = control.GetType(); BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance; MethodInfo method = type.GetMethod("OnPaint", flags); method.Invoke(control, new object[] { pea }); return bitmap; }
I have two Problems. The first:
The button on the top is the result of my funktion, but should look like the bottom-button.
The second problem is, that this does not work for all Controls, for ProgressBar for example it does not work.
Does someone know why my funktion works not correct, or maybe a third way to paint a Control to a Bitmap?
Thanks
Ludwig