关于RTF提取图片和文字的方法。

存储rtf有时候需要实现RTF文字和图片分离。rtf文字可以通过RICHTEXTBOX.TEXT而获取。但是图片的话需要分离。

实现原理:

原来存储在RTF格式中的图片数据(图片数据位置请参阅RTF格式研究这篇文章)是把原图片的16进制数据直接变成ascii字符数据嵌入RTF文件中的

也就是:

&H432D(一共16bit数据长度)变成了"432D"(一共32bit数据)字符串存储在文件中,我们要做的是把字符串再转回16进制数

========================================================

只要定义一个字节数组B() as byte

每两个字符从RTF图片数据中读出//2个字符16bit大小

通过val等函数转换成数值//十进制,因为16进制在vb比较难操作,所以选择数值相同的十进制,存进去变量以后的2进制是一样的

存储在B(i)中//8bit(把2个字符压成一个字符拉....)

然后用vb的二进制方式binary

写入整个B()数组到文件

然后通过任何图片程序就可以读取拉 

分离代码如下:

       string cmd = "select QUESTION_CONTENT from TB_QUESTION where QUESTION_ID='TK11042900284'";
                    tempStr = SQLDAL.OracleHelper.GetSingle(cmd);
                    //数据库读出的RTF字符串。
                    string _RtfText = tempStr;
                    IList<string> _ImageList = new List<string>();
                    while (true)
                    {
                        int _Index = _RtfText.IndexOf("pichgoal");
                        if (_Index == -1) break;
                        _RtfText = _RtfText.Remove(0, _Index + 8);

                        _Index = _RtfText.IndexOf("/r/n");

                    
                        _RtfText = _RtfText.Remove(0, _Index);
                    
                     

                        _Index = _RtfText.IndexOf("}");
                        _ImageList.Add(_RtfText.Substring(0, _Index).Replace("/r/n", ""));

                        _RtfText = _RtfText.Remove(0, _Index);

                    }

                
                    Byte[] buffer;
                    buffer = null;
                    for (int i = 0; i != _ImageList.Count; i++)
                    {
                     //   System.IO.FileStream _File = new FileStream(System.Windows.Forms.Application.StartupPath + "//" + i.ToString() + ".dat", System.IO.FileMode.Create);
                        int _Count = _ImageList[i].Length / 2;
                         buffer = new Byte[_ImageList[i].Length/2];

                        for (int z = 0; z != _Count; z++)
                        {
                            string _TempText = _ImageList[i][z * 2].ToString() + _ImageList[i][(z * 2) + 1].ToString();
                        //    _File.WriteByte(Convert.ToByte(_TempText, 16));
                            buffer[z] = Convert.ToByte(_TempText, 16); 
                        }

                   //    _File.Close();
                    }
                    MemoryStream ms = new MemoryStream(buffer);
                    Image _a = Image.FromStream(ms);
                  //  Bitmap _a = new Bitmap(Application.StartupPath + "//" + "0.dat");
                    if (_a != null)
                    {
                        pictureBox1.Image = _a;
                     
                    }
                    else
                    {
                        MessageBox.Show("as");
                    }

 

你可能感兴趣的:(String,存储,buffer,byte,rtf,imagelist)