RTF文档导成图片

您所在的位置:网站首页 rtf转换成pdf RTF文档导成图片

RTF文档导成图片

2024-07-08 07:11:08| 来源: 网络整理| 查看: 265

  我们知道在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(0, this.m_PrintableRtf.TextLength, rect, rect, e.Graphics);    }    bit.Save(@"D: tf.bmp");}


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭