473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Writing a DrawToBitmap function for the RichTextBox control

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

Nov 10 '06 #1
4 8178
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.Heigh t,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
Nov 10 '06 #2
Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Hei ght,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.

DeveloperX wrote:
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.Heigh t,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
Nov 10 '06 #3
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.Hei ght);
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00C C0020
/*SRCCOPY*/);
ReleaseDC(this.pictureBox1.Handle, srcDC);
g.ReleaseHdc(bmDC);
g.Dispose();

DeveloperX wrote:
Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Hei ght,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.

DeveloperX wrote:
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.Heigh t,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
Nov 10 '06 #4
DeveloperX,

Thanks for the help! I finally got that implementation to work. Thanks
for your help.
DeveloperX wrote:
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.Hei ght);
Graphics g=Graphics.FromImage(bm);
System.IntPtr bmDC=g.GetHdc();
BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00C C0020
/*SRCCOPY*/);
ReleaseDC(this.pictureBox1.Handle, srcDC);
g.ReleaseHdc(bmDC);
g.Dispose();

DeveloperX wrote:
Oops replace the bitblt line with

BitBlt(gi2,0,0,richTextBox1.Width,richTextBox1.Hei ght,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.

DeveloperX wrote:
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.Heigh t,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
Nov 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: vanvee | last post by:
Hi I have a user control that contains a RichTextBox in vb.net. In my program, I create multiple instances of this control (with the RichTextBox being in each one), that appear one above the...
1
by: Nathan Carroll | last post by:
In an mdi environment I constructed a child for with a richtextbox control that is used to load .rtf's. This works fine on the intiatial load of the form but when for is closed and reopened later...
6
by: powella | last post by:
Hello All, I've recently discovered the DrawToBitmap method hidden away in the Control class (in .NET 2.0). What a fantastic addition to the class. I'm having some degree of difficulty using...
1
by: GM | last post by:
Hello, I have visual studio 2005 on XP SP2. I have a simple sample application using a webbrowser and 2 buttons. Button1 loads the page Button2 saves the screenshot But for some reason the...
0
by: Prosperz | last post by:
Hi, I would like if there is a way to use DrawToBitmap with a WebBrowser control in Framework 2.0. In MSDN I found this method is not implemented for this control. Thanks.
2
by: NickP | last post by:
Hi there, I have a form with a few controls on, some of them are controls I have made. Anyway, when I call DrawToBitmap on the form all looks fine except for the controls I have made, they have...
0
by: Joey | last post by:
I want to use DrawToBitmap to generate a "thumbnail" graphic reflecting a particular web page. The following code successfully generates a graphic of the web page being viewed in the WebBrowser...
1
by: omlac omlac | last post by:
Hi, im having a problem with drawtobitmap in C# when i call the method its not printing the contents of a control. in my case richtext format, my code is below; Bitmap bi = new...
4
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a multiline RichTextBox that I use for data logging. I'm concerned about how the AppendText method reacts when over time the maximum number of characters have been added. Do the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.