Writing a DrawToBitmap function for the RichTextBox control

T

Technical

Greetings,

I am currently working on a project that requires that the contents of
a RichTextBox control be drawn to a bitmap. I have attempted nearly
everything including using calls into the Win32 API BitBlt function. Is
t here a way to perform this feat without sifting through the contents
of the control and drawing character-by-character? (Note that I have
seen just about every forum message on the web regarding this topic,
and have attempt many different approaches: PrintWindow, overloads,
etc...)

Thanks for your help
 
D

DeveloperX

Assuming you're only interested in the visible bit, it's easy with
interop. There's also a way to blit in dotnet, but I can't find where
ever I saw it.

using System.Runtime.InteropServices;

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth,
int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

Got a form with a rtb and this creates a copy into a picturebox. You
might have to tweak it a bit, but the basics are there.

Graphics g = richTextBox1.CreateGraphics();
Graphics g2 = pictureBox1.CreateGraphics();
IntPtr gi = g.GetHdc();
IntPtr gi2 = g2.GetHdc();
BitBlt(gi2,0,0,pictureBox1.Width,pictureBox1.Height,gi,0,0,0x00CC0020);
g.Dispose();
g2.Dispose();

sorry I've not got more time to look into it.
 
D

DeveloperX

Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Height,gi,0,0,0x00CC0020);

btw the oocc0020 refers to copy.

There are some issues with releasing (disposing) the Graphics objects
which I haven't looked into, but that should be an easier to sort out.
Assuming you're only interested in the visible bit, it's easy with
interop. There's also a way to blit in dotnet, but I can't find where
ever I saw it.

using System.Runtime.InteropServices;

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth,
int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

Got a form with a rtb and this creates a copy into a picturebox. You
might have to tweak it a bit, but the basics are there.

Graphics g = richTextBox1.CreateGraphics();
Graphics g2 = pictureBox1.CreateGraphics();
IntPtr gi = g.GetHdc();
IntPtr gi2 = g2.GetHdc();
BitBlt(gi2,0,0,pictureBox1.Width,pictureBox1.Height,gi,0,0,0x00CC0020);
g.Dispose();
g2.Dispose();

sorry I've not got more time to look into it.


Greetings,

I am currently working on a project that requires that the contents of
a RichTextBox control be drawn to a bitmap. I have attempted nearly
everything including using calls into the Win32 API BitBlt function. Is
t here a way to perform this feat without sifting through the contents
of the control and drawing character-by-character? (Note that I have
seen just about every forum message on the web regarding this topic,
and have attempt many different approaches: PrintWindow, overloads,
etc...)

Thanks for your help
 
D

DeveloperX

Ok, I wasn't a million miles off, Bob Powell gives us the answer at
http://www.bobpowell.net/capture.htm
Here's his code, GetDC and ReleaseDC are imported along with BitBlt.

System.IntPtr srcDC=GetDC(this.pictureBox1.Handle);
Bitmap bm=new
Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00CC0020
/*SRCCOPY*/);
ReleaseDC(this.pictureBox1.Handle, srcDC);
g.ReleaseHdc(bmDC);
g.Dispose();


Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Height,gi,0,0,0x00CC0020);

btw the oocc0020 refers to copy.

There are some issues with releasing (disposing) the Graphics objects
which I haven't looked into, but that should be an easier to sort out.
Assuming you're only interested in the visible bit, it's easy with
interop. There's also a way to blit in dotnet, but I can't find where
ever I saw it.

using System.Runtime.InteropServices;

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth,
int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

Got a form with a rtb and this creates a copy into a picturebox. You
might have to tweak it a bit, but the basics are there.

Graphics g = richTextBox1.CreateGraphics();
Graphics g2 = pictureBox1.CreateGraphics();
IntPtr gi = g.GetHdc();
IntPtr gi2 = g2.GetHdc();
BitBlt(gi2,0,0,pictureBox1.Width,pictureBox1.Height,gi,0,0,0x00CC0020);
g.Dispose();
g2.Dispose();

sorry I've not got more time to look into it.


Greetings,

I am currently working on a project that requires that the contents of
a RichTextBox control be drawn to a bitmap. I have attempted nearly
everything including using calls into the Win32 API BitBlt function. Is
t here a way to perform this feat without sifting through the contents
of the control and drawing character-by-character? (Note that I have
seen just about every forum message on the web regarding this topic,
and have attempt many different approaches: PrintWindow, overloads,
etc...)

Thanks for your help
 
T

Technical

DeveloperX,

Thanks for the help! I finally got that implementation to work. Thanks
for your help.

Ok, I wasn't a million miles off, Bob Powell gives us the answer at
http://www.bobpowell.net/capture.htm
Here's his code, GetDC and ReleaseDC are imported along with BitBlt.

System.IntPtr srcDC=GetDC(this.pictureBox1.Handle);
Bitmap bm=new
Bitmap(this.pictureBox1.Width,this.pictureBox1.Height);
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00CC0020
/*SRCCOPY*/);
ReleaseDC(this.pictureBox1.Handle, srcDC);
g.ReleaseHdc(bmDC);
g.Dispose();


Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Height,gi,0,0,0x00CC0020);

btw the oocc0020 refers to copy.

There are some issues with releasing (disposing) the Graphics objects
which I haven't looked into, but that should be an easier to sort out.
Assuming you're only interested in the visible bit, it's easy with
interop. There's also a way to blit in dotnet, but I can't find where
ever I saw it.

using System.Runtime.InteropServices;

[DllImport("gdi32.dll")]
static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int
nWidth,
int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

Got a form with a rtb and this creates a copy into a picturebox. You
might have to tweak it a bit, but the basics are there.

Graphics g = richTextBox1.CreateGraphics();
Graphics g2 = pictureBox1.CreateGraphics();
IntPtr gi = g.GetHdc();
IntPtr gi2 = g2.GetHdc();
BitBlt(gi2,0,0,pictureBox1.Width,pictureBox1.Height,gi,0,0,0x00CC0020);
g.Dispose();
g2.Dispose();

sorry I've not got more time to look into it.



Technical wrote:
Greetings,

I am currently working on a project that requires that the contents of
a RichTextBox control be drawn to a bitmap. I have attempted nearly
everything including using calls into the Win32 API BitBlt function. Is
t here a way to perform this feat without sifting through the contents
of the control and drawing character-by-character? (Note that I have
seen just about every forum message on the web regarding this topic,
and have attempt many different approaches: PrintWindow, overloads,
etc...)

Thanks for your help
 

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