473,402 Members | 2,050 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,402 software developers and data experts.

Need to save Word Doc or RTF as BMP

Hi
I know how to use the bitmap and graphics objects to create bitmaps and
write text on them with the drawString method. I now need to create some BMPs
of word or RTF docs. What would be the best approach here as I need to
maintain most if not all of the formatting. The docs will be about 3
paragraphs each.

Should I save the docs to rtf, then parse the rtf into the draw string
method? There has to be an easier way.

Thank you
Mar 2 '06 #1
3 4561
Ok make a new windows Form, then add a RichTextBox to it. Then render
your RTF on the richtextbox and screen dump the Form (you never have to
show it)

Here is the code to dump a Form:

public static void CaptureScreen(Form form, string saveLocation)
{
Graphics g1 = form.CreateGraphics();
Image image = new Bitmap(form.ClientRectangle.Width,
form.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(image);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, form.ClientRectangle.Width,
form.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
image.Save(saveLocation, System.Drawing.Imaging.ImageFormat.Jpeg);
image.Dispose();
}

[System.Runtime.InteropServices.DllImportAttribute( "gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Mar 2 '06 #2
Thank you Vooose

"vooose" wrote:
Ok make a new windows Form, then add a RichTextBox to it. Then render
your RTF on the richtextbox and screen dump the Form (you never have to
show it)

Here is the code to dump a Form:

public static void CaptureScreen(Form form, string saveLocation)
{
Graphics g1 = form.CreateGraphics();
Image image = new Bitmap(form.ClientRectangle.Width,
form.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(image);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, form.ClientRectangle.Width,
form.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
image.Save(saveLocation, System.Drawing.Imaging.ImageFormat.Jpeg);
image.Dispose();
}

[System.Runtime.InteropServices.DllImportAttribute( "gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***

Mar 2 '06 #3
Yes, I know, I'm more than a year late on seeing this. I've been looking for
something like this for a while.

Unfortunately, vooose, this only works if the RichTextBox control is visible
and on top of all other controls/windows (topmost). It's the programmatic
equivalent of hitting Alt+PrntScrn. The part where you say "you never have
to show it" isn't correct.

I'm looking for a way to quickly convert an RTF file into a PNG file (or
bitmap or whatever). I was hoping this would help, but it's not adequate.

Is there some other way to redirect or copy a RichTextBox's rendering
(regardless of the Height) to a Bitmap object, without the RichTextBox being
visible? I was hoping to make a small and fast console app that I can add to
an RTF file's open-with menu.

"vooose" wrote:
Ok make a new windows Form, then add a RichTextBox to it. Then render
your RTF on the richtextbox and screen dump the Form (you never have to
show it)

Here is the code to dump a Form:

public static void CaptureScreen(Form form, string saveLocation)
{
Graphics g1 = form.CreateGraphics();
Image image = new Bitmap(form.ClientRectangle.Width,
form.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(image);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, form.ClientRectangle.Width,
form.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
image.Save(saveLocation, System.Drawing.Imaging.ImageFormat.Jpeg);
image.Dispose();
}

[System.Runtime.InteropServices.DllImportAttribute( "gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Sep 29 '07 #4

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

Similar topics

2
by: Martin Lucas-Smith | last post by:
I am trying to use PHP's COM support to open a URL from within MS Word then save the document. I am using PHP5.0.3/Apache2/WindowsXP. phpinfo() confirms that COM support is enabled. ...
4
by: bborden | last post by:
When I save a document in Word that I have transferred from VBA to Word I notice that the "Save As" file name is for, instance, "December 4.doc". Since the date is the first field I print on the...
2
by: zheng | last post by:
ÇçÌì I have a word document and it has a picture object, I want save the picture object as other single picture file by C# program. I have a idea, by clipboard, copy the picture to clipboard, and...
8
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
1
by: AVL | last post by:
Hi, I've a requirement in which I need to show a word document in the web page, allow the user to edit the document and then save the document ina particular location, How can I acheive in asp.net...
3
by: ljungers | last post by:
I need to make some changes to a Query/select/print report using word application. What I need to do is change the way Word is called yet keep the process the same. Word is used so changes can be...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: =?Utf-8?B?QmFkaXM=?= | last post by:
Hi, I'm using the bellow code to mail merge a word document and it's working fine, where I'm openning "improvement.doc" which is just word document that contain with some text and the merge...
2
by: gusa992000 | last post by:
Hello I am trying the following problem: A web app in jsp and java has to open a microsoft word document directly in the browser. This part is easy and i have done it. The user then can...
1
by: =?Utf-8?B?S2ltIFM=?= | last post by:
I have a word document I only want to printout ( not save as file ) and I use Word printout method in backgrund but my XP operativ i so slow that a "dummy" dociment also get stored. which...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.