如何提取richtextbox中的图片并保存到一个文件里?

无爱大叔 2008-12-23 04:33:07
数据库中的某个字段是blob类型,其中的内容为图文混排。
如何将其中的文字和图片都分开单独存成文件?
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。
...全文
1708 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
pastar 2012-10-23
  • 打赏
  • 举报
回复
好像有点小错误,读取应该这样:

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).Replace("\r\n", "");

_RtfText = _RtfText.Remove(0, _Index);

}
allan3830283178 2012-07-28
  • 打赏
  • 举报
回复
你不是已经把文字提取出来了么,先把RICHTEXTbox里面的文字删了,再存图片,
binbin394586176 2012-05-25
  • 打赏
  • 举报
回复
看看哦
龟宝宝 2011-02-09
  • 打赏
  • 举报
回复
如何在把 图片和文本整合到一起呢
dust_ying 2010-12-22
  • 打赏
  • 举报
回复
请问下,这个图片怎么获得的,把.dat改成.jpg或者其他的后缀就行了吗?谢谢![Quote=引用 7 楼 xhbmj 的回复:]

引用 4 楼 zgke 的回复:
C# codestring_RtfText=richTextBox1.SelectedRtf;
IList<string>_ImageList=newList<string>();while(true)
{int_Index=_RtfText.IndexOf("pichgoal");if(_Index==-1)break;
_RtfText=_RtfTe……
[/Quote]
zgke 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xhbmj 的回复:]
引用 4 楼 zgke 的回复:
C# codestring_RtfText=richTextBox1.SelectedRtf;
IList <string>_ImageList=newList <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));

[/Quote]

如果你想获取原始大小 就不能直接寻找 pichgoal

改成寻找 pict

后面的 picw pich 是图形的大小 你读出来后面是数字以'\'或'\r\n'结束
然后再寻找pichgoal

读出字符 生成图形--打开图形然后 绘制大小.

无爱大叔 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zgke 的回复:]
C# codestring_RtfText=richTextBox1.SelectedRtf;
IList<string>_ImageList=newList<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)…
[/Quote]
非常感谢,可以出图片了;不过有个问题就是图片的大小比例和原来不一致,可以解决吗?
另外我看到了字符串是从pichgoal到第一个}为一个图片文件分割,这样分割的原理是什么?
laidon 2008-12-23
  • 打赏
  • 举报
回复
提取richTextBox中的图片要根据楼主插入图片的方法而定,不同的图片写入方法要用不同的保存方法
楼主的方法适合于用Clipboard方法写入的
zgke 2008-12-23
  • 打赏
  • 举报
回复
保存的文件.dat 你改个名字看看
zgke 2008-12-23
  • 打赏
  • 举报
回复


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());



现写的~~可能有错误
zgke 2008-12-23
  • 打赏
  • 举报
回复
richTextBox1.SelectedRtf

分析
无爱大叔 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cpio 的回复:]

[/Quote]
大哥能不能说句话
cpio 2008-12-23
  • 打赏
  • 举报
回复

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧