当前位置: 代码迷 >> C# >> richtextbox经过LoadFile显示图片提示文件格式无效,等
  详细解决方案

richtextbox经过LoadFile显示图片提示文件格式无效,等

热度:79   发布时间:2016-05-05 03:59:00.0
richtextbox通过LoadFile显示图片提示文件格式无效,急等!
代码如下:
        private void button2_Click(object sender, EventArgs e)//richtextbox图片存入数据库
        {
            MemoryStream STM = new MemoryStream();
            this.richTextBox1.SaveFile(STM, RichTextBoxStreamType.RichText);
            byte[] bWrite = STM.ToArray();

            DBManager db = new DBManager();
            string sqlStr = "insert into BLJ_Test(ID,rtfimage) values('1','" + bWrite + "')";
            db.UpdataDB(sqlStr);
            MessageBox.Show("插入数据成功");
            richTextBox1.Text = "";

        }

        private void button3_Click(object sender, EventArgs e)//数据库图片读取
        {

            string sqlStr = "select rtfimage from BLJ_Test where ID=0";
            SqlCommand myCmd = new SqlCommand(sqlStr, DBManager.sqlConn);
            DBManager.sqlConn.Open();
            SqlDataReader dt = myCmd.ExecuteReader();
            dt.Read();
            byte[] bReader = (byte[])dt["rtfimage"];
            MemoryStream mstream = new MemoryStream(bReader, false);
            RichTextBox rtb = new RichTextBox();
            rtb.LoadFile(mstream, RichTextBoxStreamType.RichText);//就是这个位置报错
            MessageBox.Show("test");
            richTextBox1.SelectedRtf = rtb.Rtf;
            dt.Close();
            DBManager.sqlConn.Close();
        }
报错信息如下:

有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.ArgumentException: 文件格式无效。
   在 System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
   在 System.Windows.Forms.RichTextBox.LoadFile(Stream data, RichTextBoxStreamType fileType)
   在 BLJ.Test1.button3_Click(Object sender, EventArgs e) 位置 e:\WorkSpace\BLJ\BLJ\BLJ\BLJ\Test1.cs:行号 57
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
------解决思路----------------------
string sqlStr = "insert into BLJ_Test(ID,rtfimage) values('1','" + bWrite + "')";
晕死,
 bWrite是个字节数组
你这样拼接进字符串里,你确定存进去的是个什么玩意吗

要存image类型的字段,必须参数化传值
------解决思路----------------------
哪些列不想显示,直接设置
dataGridView1.Columns[index].visible=false;
就行了