RTF文档导成图片

  我们知道在VS.NET环境下有一个RichTextBox控件可以很好的显示Rtf文档,可是有的时候我们需要以图形的形式来显示一个Rtf格式的文档.但RichTextBox并不支持把显示的内容存成图片,经过查找资料我得到了如下的代码,使用这些代码就可以把以RichTextBox显示Rtf格式的内容很好的显示为图片,或使用到Graphics中. 

 

     internal   class  PrintableRtf:RichTextBox
    
{
        
//Convert the unit used by the .NET framework (1/100 inch) 
        
//and the unit used by Win32 API calls (twips 1/1440 inch)
        private const double anInch = 14.4;

        [StructLayout(LayoutKind.Sequential)]
        
private struct RECT
        
{
            
public int Left;
            
public int Top;
            
public int Right;
            
public int Bottom;
        }


        [StructLayout(LayoutKind.Sequential)]
        
private struct CHARRANGE
        
{
            
public int cpMin;         //First character of range (0 for start of doc)
            public int cpMax;           //Last character of range (-1 for end of doc)
        }


        [StructLayout(LayoutKind.Sequential)]
        
private struct FORMATRANGE
        
{
            
public IntPtr hdc;             //Actual DC to draw on
            public IntPtr hdcTarget;       //Target DC for determining text formatting
            public RECT rc;                //Region of the DC to draw to (in twips)
            public RECT rcPage;            //Region of the whole DC (page size) (in twips)
            public CHARRANGE chrg;         //Range of text to draw (see earlier declaration)
        }


        
private const int WM_USER = 0x0400;
        
private const int EM_FORMATRANGE = WM_USER + 57;

        [DllImport(
"USER32.dll")]
        
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

        
// Render the contents of the RichTextBox for printing
        
//    Return the last character printed + 1 (printing start from this point for next page)
        public int Print(int charFrom, int charTo, RectangleF marginBounds, RectangleF pageBounds, Graphics g)
        
{
            
//Calculate the area to render and print
            RECT rectToPrint;
            rectToPrint.Top 
= (int)(marginBounds.Top * anInch);
            rectToPrint.Bottom 
= (int)(marginBounds.Bottom * anInch);
            rectToPrint.Left 
= (int)(marginBounds.Left * anInch);
            rectToPrint.Right 
= (int)(marginBounds.Right * anInch);

            
//Calculate the size of the page
            RECT rectPage;
            rectPage.Top 
= (int)(pageBounds.Top * anInch);
            rectPage.Bottom 
= (int)(pageBounds.Bottom * anInch);
            rectPage.Left 
= (int)(pageBounds.Left * anInch);
            rectPage.Right 
= (int)(pageBounds.Right * anInch);

            IntPtr hdc 
= g.GetHdc();

            FORMATRANGE fmtRange;
            fmtRange.chrg.cpMax 
= charTo;                //Indicate character from to character to 
            fmtRange.chrg.cpMin = charFrom;
            fmtRange.hdc 
= hdc;                            //Use the same DC for measuring and rendering
            fmtRange.hdcTarget = hdc;                    //Point at printer hDC
            fmtRange.rc = rectToPrint;                    //Indicate the area on page to print
            fmtRange.rcPage = rectPage;                    //Indicate size of page

            IntPtr res 
= IntPtr.Zero;

            IntPtr wparam 
= IntPtr.Zero;
            wparam 
= new IntPtr(1);

            
//Get the pointer to the FORMATRANGE structure in memory
            IntPtr lparam = IntPtr.Zero;
            lparam 
= Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            Marshal.StructureToPtr(fmtRange, lparam, 
false);

            
//Send the rendered data for printing 
            res = SendMessage(this.Handle, EM_FORMATRANGE, wparam, lparam);

            
//Free the block of memory allocated
            Marshal.FreeCoTaskMem(lparam);

            
//Release the device context handle obtained by a previous call
            g.ReleaseHdc(hdc);

            
//Return last + 1 character printer
            return res.ToInt32();
        }

    }

Rectangle rect 
=   new  Rectangle( 0 , 0 800 , 600 );

using  (Bitmap bit  =   new  Bitmap( 800 , 600 ))
{
    
using (Graphics g = Graphics.FromImage(bit))
    

        
int EndPoint = m_PrintableRtf.Print(0this.m_PrintableRtf.TextLength, rect, rect, e.Graphics);
    }

    bit.Save(
@"D: tf.bmp");
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
RTF文档格式,即富文本格式,是一种通用的文件格式,用于在不同操作系统和文字处理软件之间交换文本文件。RTF文件可以包含文本、图像、表格、超链接等元素,可以通过多种文字处理软件打开和编辑。 RTF文档格式白皮书是关于RTF文档格式的详细说明和指导的文件。这份白皮书通常包含以下内容: 1. RTF的定义和历史:白皮书会介绍RTF文档格式的起源和发展历程,包括RTF的创造者和不同版本的特点。 2. RTF的核心结构:白皮书会详细解释RTF文档的核心结构,包括RTF头部、文本内容、格式控制指令和对象嵌入等部分。它还会解释这些组成部分的功能和使用方法。 3. RTF的格式控制指令:白皮书会列举和解释RTF文档中可用的格式控制指令,例如字体、颜色、对齐方式、段落格式等。它还会提供示例和语法规则,方便用户理解和使用这些指令。 4. RTF与其他文件格式的换:白皮书可能探讨RTF与其他常见文件格式(如HTML、DOC、PDF)之间的换方法和技巧。它会介绍换过程中可能遇到的问题和解决方案。 5. RTF的应用场景和优势:白皮书会介绍RTF文档格式在不同领域和场景中的应用。它可能提供实际案例,并强调RTF格式的跨平台兼容性和可扩展性的优势。 总之,RTF文档格式白皮书是一份对RTF格式进行详尽解释和说明的文件,旨在帮助用户深入了解和充分利用RTF格式进行文档处理和交换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值