日期:2014-05-18  浏览次数:21413 次

如何提取richtextbox中的图片并保存到一个文件里?
数据库中的某个字段是blob类型,其中的内容为图文混排。
如何将其中的文字和图片都分开单独存成文件?
 
C# code
private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.Copy(); 
            string str = richTextBox1.Text;//获取文本部分
            Image img1 = (Image)Clipboard.GetData(System.Windows.Forms.DataFormats.Bitmap);//获取图片部分
            if (img1 != null)
            {
                img1.Save(Application.ExecutablePath + "a.bmp");
            }
        }

如上代码中img1始终是null。

------解决方案--------------------
C# code


     string _RtfText=richTextBox1.SelectedRtf;
            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");
                
                int _Temp = Convert.ToInt32(_RtfText.Substring(0, _Index));
                _RtfText = _RtfText.Remove(0, _Index);
                _RtfText = _RtfText.Replace("\r\n", "");

                _Index = _RtfText.IndexOf("}");
                _ImageList.Add(_RtfText.Substring(0, _Index));

                _RtfText = _RtfText.Remove(0, _Index);                
                
            }

            for (int i = 0; i != _ImageList.Count; i++)
            {
                System.IO.FileStream _File = new FileStream(Application.StartupPath + "\\" + i.ToString() + ".dat", System.IO.FileMode.Create);
                int _Count=_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));
                }

                _File.Close();
            }           


            MessageBox.Show(_ImageList.Count.ToString());