天天看點

RTF文檔導成圖檔

  我們知道在VS.NET環境下有一個RichTextBox控件可以很好的顯示Rtf文檔,可是有的時候我們需要以圖形的形式來顯示一個Rtf格式的文檔.但RichTextBox并不支援把顯示的内容存成圖檔,經過查找資料我得到了如下的代碼,使用這些代碼就可以把以RichTextBox顯示Rtf格式的内容很好的顯示為圖檔,或使用到Graphics中. 

RTF文檔導成圖檔

     internal   class  PrintableRtf:RichTextBox

RTF文檔導成圖檔
RTF文檔導成圖檔

     ... {

RTF文檔導成圖檔

        //Convert the unit used by the .NET framework (1/100 inch) 

RTF文檔導成圖檔

        //and the unit used by Win32 API calls (twips 1/1440 inch)

RTF文檔導成圖檔

        private const double anInch = 14.4;

RTF文檔導成圖檔
RTF文檔導成圖檔

        [StructLayout(LayoutKind.Sequential)]

RTF文檔導成圖檔

        private struct RECT

RTF文檔導成圖檔
RTF文檔導成圖檔

        ...{

RTF文檔導成圖檔

            public int Left;

RTF文檔導成圖檔

            public int Top;

RTF文檔導成圖檔

            public int Right;

RTF文檔導成圖檔

            public int Bottom;

RTF文檔導成圖檔

        }

RTF文檔導成圖檔
RTF文檔導成圖檔

        [StructLayout(LayoutKind.Sequential)]

RTF文檔導成圖檔

        private struct CHARRANGE

RTF文檔導成圖檔
RTF文檔導成圖檔

        ...{

RTF文檔導成圖檔

            public int cpMin;         //First character of range (0 for start of doc)

RTF文檔導成圖檔

            public int cpMax;           //Last character of range (-1 for end of doc)

RTF文檔導成圖檔

        }

RTF文檔導成圖檔
RTF文檔導成圖檔

        [StructLayout(LayoutKind.Sequential)]

RTF文檔導成圖檔

        private struct FORMATRANGE

RTF文檔導成圖檔
RTF文檔導成圖檔

        ...{

RTF文檔導成圖檔

            public IntPtr hdc;             //Actual DC to draw on

RTF文檔導成圖檔

            public IntPtr hdcTarget;       //Target DC for determining text formatting

RTF文檔導成圖檔

            public RECT rc;                //Region of the DC to draw to (in twips)

RTF文檔導成圖檔

            public RECT rcPage;            //Region of the whole DC (page size) (in twips)

RTF文檔導成圖檔

            public CHARRANGE chrg;         //Range of text to draw (see earlier declaration)

RTF文檔導成圖檔

        }

RTF文檔導成圖檔
RTF文檔導成圖檔

        private const int WM_USER = 0x0400;

RTF文檔導成圖檔

        private const int EM_FORMATRANGE = WM_USER + 57;

RTF文檔導成圖檔
RTF文檔導成圖檔

        [DllImport("USER32.dll")]

RTF文檔導成圖檔

        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

RTF文檔導成圖檔
RTF文檔導成圖檔

        // Render the contents of the RichTextBox for printing

RTF文檔導成圖檔

        //    Return the last character printed + 1 (printing start from this point for next page)

RTF文檔導成圖檔

        public int Print(int charFrom, int charTo, RectangleF marginBounds, RectangleF pageBounds, Graphics g)

RTF文檔導成圖檔
RTF文檔導成圖檔

        ...{

RTF文檔導成圖檔

            //Calculate the area to render and print

RTF文檔導成圖檔

            RECT rectToPrint;

RTF文檔導成圖檔

            rectToPrint.Top = (int)(marginBounds.Top * anInch);

RTF文檔導成圖檔

            rectToPrint.Bottom = (int)(marginBounds.Bottom * anInch);

RTF文檔導成圖檔

            rectToPrint.Left = (int)(marginBounds.Left * anInch);

RTF文檔導成圖檔

            rectToPrint.Right = (int)(marginBounds.Right * anInch);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Calculate the size of the page

RTF文檔導成圖檔

            RECT rectPage;

RTF文檔導成圖檔

            rectPage.Top = (int)(pageBounds.Top * anInch);

RTF文檔導成圖檔

            rectPage.Bottom = (int)(pageBounds.Bottom * anInch);

RTF文檔導成圖檔

            rectPage.Left = (int)(pageBounds.Left * anInch);

RTF文檔導成圖檔

            rectPage.Right = (int)(pageBounds.Right * anInch);

RTF文檔導成圖檔
RTF文檔導成圖檔

            IntPtr hdc = g.GetHdc();

RTF文檔導成圖檔
RTF文檔導成圖檔

            FORMATRANGE fmtRange;

RTF文檔導成圖檔

            fmtRange.chrg.cpMax = charTo;                //Indicate character from to character to 

RTF文檔導成圖檔

            fmtRange.chrg.cpMin = charFrom;

RTF文檔導成圖檔

            fmtRange.hdc = hdc;                            //Use the same DC for measuring and rendering

RTF文檔導成圖檔

            fmtRange.hdcTarget = hdc;                    //Point at printer hDC

RTF文檔導成圖檔

            fmtRange.rc = rectToPrint;                    //Indicate the area on page to print

RTF文檔導成圖檔

            fmtRange.rcPage = rectPage;                    //Indicate size of page

RTF文檔導成圖檔
RTF文檔導成圖檔

            IntPtr res = IntPtr.Zero;

RTF文檔導成圖檔
RTF文檔導成圖檔

            IntPtr wparam = IntPtr.Zero;

RTF文檔導成圖檔

            wparam = new IntPtr(1);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Get the pointer to the FORMATRANGE structure in memory

RTF文檔導成圖檔

            IntPtr lparam = IntPtr.Zero;

RTF文檔導成圖檔

            lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));

RTF文檔導成圖檔

            Marshal.StructureToPtr(fmtRange, lparam, false);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Send the rendered data for printing 

RTF文檔導成圖檔

            res = SendMessage(this.Handle, EM_FORMATRANGE, wparam, lparam);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Free the block of memory allocated

RTF文檔導成圖檔

            Marshal.FreeCoTaskMem(lparam);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Release the device context handle obtained by a previous call

RTF文檔導成圖檔

            g.ReleaseHdc(hdc);

RTF文檔導成圖檔
RTF文檔導成圖檔

            //Return last + 1 character printer

RTF文檔導成圖檔

            return res.ToInt32();

RTF文檔導成圖檔

        }

RTF文檔導成圖檔

    }

RTF文檔導成圖檔
RTF文檔導成圖檔

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

RTF文檔導成圖檔
RTF文檔導成圖檔

using  (Bitmap bit  =   new  Bitmap( 800 , 600 ))

RTF文檔導成圖檔
RTF文檔導成圖檔

... {

RTF文檔導成圖檔

    using (Graphics g = Graphics.FromImage(bit))

RTF文檔導成圖檔
RTF文檔導成圖檔

    ...{ 

RTF文檔導成圖檔

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

RTF文檔導成圖檔

    }

RTF文檔導成圖檔

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

RTF文檔導成圖檔

}

RTF文檔導成圖檔

繼續閱讀