Capture Control in Bitmap?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello!

I would like copy my control in a bitmap and after in a jpeg.
I have an error of gdi!
This is my code

System.IntPtr intPtr = this.RichTextBox.Handle;
System.Drawing.Image img = Bitmap.FromHbitmap(intPtr);
img.Save(@"Mypath\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

Have you got any idea?

Best regards,

Wavemill
 
wavemill said:
I would like copy my control in a bitmap and after in a jpeg.
I have an error of gdi!
This is my code

System.IntPtr intPtr = this.RichTextBox.Handle;
System.Drawing.Image img = Bitmap.FromHbitmap(intPtr);
img.Save(@"Mypath\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

That handle returned by the RichTextBox is not a HBitmap, unsurprisingly,
so this doesn't work. The FromHBitmap method works only on handles that
actually belong to a Windows bitmap.

There are two ways of solving your problem, depending on your .NET
version. In .NET 2, there's a new method on the Control, called
DrawToBitmap - this is obviously the easiest thing to use if you're on
..NET 2. In .NET 1, you'll have to reproduce the functionality of that
method yourself. I have recently done that for a blog article - see
http://www.sturmnet.org/blog/archives/2005/08/26/explorer-rubber-band2/ ,
right down at the bottom in the comments is the .NET implementation you
need and the sample source code tells you all about using it.


Oliver Sturm
 
Back
Top