Control to an Image

G

Guest

How would I got about capturing the drawn area of a control into a Image? I can print the control but I want to store whats drawn on screen at any time for a custom graphing control for printing later. So far I have had no luck determining how I would do this. Using the CreateGraphic method of a control has not yielded results.

Thanks
Christian
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?Q2hyaXN0aWFu?= said:
How would I got about capturing the drawn area of a control into a
Image? I can print the control but I want to store whats drawn on screen
at any time for a custom graphing control for printing later. So far I
have had no luck determining how I would do this. Using the
CreateGraphic method of a control has not yielded results.

<http://www.google.com/[email protected]>
 
G

Guest

Particularily the following:

Dim Methods() As System.Reflection.MethodInfo =
ctl.GetType.GetMethods( System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)

OnPaintMethod.Invoke(ctl, New Object() {PaintEventArgs})
 
E

Eric Cadwell

Try this:

public static Bitmap ControlAsImage(Control Ctrl)

{

MethodInfo[] methods = Ctrl.GetType().GetMethods(BindingFlags.Instance |
BindingFlags.NonPublic);

if (methods != null)

{

MethodInfo onPaint = null;

for (int i = 0; i < methods.Length; i++)

{

if (methods.Name == "OnPaint")

{

onPaint = methods;

break;

}

}

if (onPaint != null)

{

Bitmap bmp = new Bitmap(Ctrl.Width, Ctrl.Height);

using (Graphics g = Graphics.FromImage(bmp))

{

PaintEventArgs pea = new PaintEventArgs(g, new Rectangle(0, 0, Ctrl.Width,
Ctrl.Height));

onPaint.Invoke(Ctrl, new object[] { pea });

}

return bmp;

}

}

return null;

}



And yes it does not work for all controls.



HTH;
Eric Cadwell
http://www.origincontrols.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top