How to create a bitmap out of a control?

G

Guest

Hello,

I'm using a control (black box control) in my form (an ActiveX/Com control).
and I need to take the control as an image.
The controls is showing some kind of graph, and I want to create a bitmap
file out of this control, so the bitmap will show what the control is showing.

I tried this:
/////////////////////////////////////////////////////////////////
AxTheControl.AxControl ctrl = new AxTheControl.AxControl();
Graphics graphics = ctrl.CreateGraphics();
IntPtr hdc = graphics.GetHdc();
try
{
Bitmap bitmap = Bitmap.FromHbitmap(hdc);
bitmap.Save("C\ControlImage.bmp");
}
catch( Exception exp )
{
MessageBox.Show(exp.Message,);
}
graphics.ReleaseHdc(hdc);
graphics.Dispose();
/////////////////////////////////////////////////////////////////

But is throws a GDI+ general error exception.


Does anybody knows how can I do that?
 
G

Guest

Hello,

I'm using a control (black box control) in my form (an ActiveX/Com control).
and I need to take the control as an image.
The controls is showing some kind of graph, and I want to create a bitmap
file out of this control, so the bitmap will show what the control is showing.

I tried this:
/////////////////////////////////////////////////////////////////
AxControl ctrl = new AxControl();
Graphics graphics = ctrl.CreateGraphics();
IntPtr hdc = graphics.GetHdc();
try
{
Bitmap bitmap = Bitmap.FromHbitmap(hdc);
bitmap.Save("ControlImage.bmp");
}
catch( Exception exp )
{
MessageBox.Show(exp.Message);
}
graphics.ReleaseHdc(hdc);
graphics.Dispose();
/////////////////////////////////////////////////////////////////

But is throws a GDI+ general error exception.


Does anybody knows how can I do that?
 
N

Nicholas Paldino [.NET/C# MVP]

Sharon,

You will want to call the DrawToBitmap method on the control, and it
will draw the controls contents to the Bitmap that you specify.

It's not working now because you are passing a device context to the
FromHBitmap method, which is a different type of handle all together.

Hope this helps.

- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
 
G

Guest

Ok, Thanks, now I see where I'm wrong in my code.

The control I'm using does not have the DrawToBitmap method, and at the MSDN
help is says: "The DrawToBitmap method is not supported for ActiveX controls.
"

Is there any other way to that?
 

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