标题:C#实现MYSQL数据库保存和读取图片,大神,求助
只看楼主
q1076912359
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-5-29
 问题点数:0 回复次数:0 
C#实现MYSQL数据库保存和读取图片,大神,求助
读取图片时,Image image = Image.FromStream(MStream);显示参数无效,求大神帮助
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using

namespace ByteImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region 定义公共的类对象及变量
        MySqlConnection sqlcon;       //声明数据库连接对象
        MySqlDataAdapter sqlda;       //声明数据桥接器对象
        DataSet myds;               //声明数据集对象
        //定义数据库连接字符串        
        #endregion
        private void Form1_Load(object sender, EventArgs e)
        {
            ShowInfo();             //显示图片信息
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //定义可选择的图片类型
            openFileDialog1.Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf";
            openFileDialog1.Title = "选择图片";
            //判断是否选择了图片
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //显示选择的图片
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.FileName != "" && textBox1.Text != "")
            {
                //添加图片信息
                if (AddInfo(textBox1.Text, openFileDialog1.FileName))
                {
                    MessageBox.Show("图片信息添加成功");
                }
            }
            ShowInfo();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {  //记录选择的用户名

            string strName = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            if (strName != "")
            {               //数据桥接器对象
                MySqlConnection sqlco = new MySqlConnection("Server=localhost;Uid=root;Password=123456;Database=test");
                sqlda = new MySqlDataAdapter("select * from tb_image where name='" + strName + "'", sqlco);
                myds = new DataSet();           //数据集对象
                sqlda.Fill(myds);                       //填充数据集
                //显示图片名称
                textBox1.Text = myds.Tables[0].Rows[0][0].ToString();
                //使用数据库中存储的二进制图片实例化内存数据流
                MemoryStream MStream = new MemoryStream((byte[])myds.Tables[0].Rows[0]["photo"]);
                Image image = Image.FromStream(MStream);   此处出错,显示参数无效
                pictureBox1.Image = image; //显示图片
              
            }

        }
        #region 添加图片信息      
        private bool AddInfo(string strName, string strImage)
        {
            sqlcon = new MySqlConnection(); //strCon
            FileStream FStream = new FileStream(strImage, FileMode.Open, FileAccess.Read);
            BinaryReader BReader = new BinaryReader(FStream);
            byte[] byteImage = BReader.ReadBytes((int)FStream.Length);
            MySqlConnection conn = new MySqlConnection("Server=localhost;Uid=root;Password=123456;Database=test");
            conn.Open();
            MySqlCommand comm = new MySqlCommand("insert into tb_image(name,photo) values(?name,?photo)", sqlcon);
            comm.Connection = conn;
            comm.Parameters.Add("?name",MySqlDbType.VarChar).Value=strName;
            comm.Parameters.Add("?photo",MySqlDbType.VarChar).Value = strImage;         
            comm.ExecuteNonQuery();
            conn.Close();
            return true;
        }
        #endregion
        #region 在DataGridView中显示图片名称      
        private void ShowInfo()
        {
         
            MySqlConnection sqlcon = new MySqlConnection("Server=localhost;Uid=root;Password=123456;Database=test");
            sqlda = new MySqlDataAdapter("select name as name from tb_image", sqlcon);
            myds = new DataSet();
            sqlda.Fill(myds);
            dataGridView1.DataSource = myds.Tables[0];
        }
        #endregion
    }
}
搜索更多相关主题的帖子: 数据库 public 图片 
2016-05-29 15:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465363-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.256391 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved