首页 > 代码库 > C#_图片存取数据库Winform

C#_图片存取数据库Winform

     #region 用于在PictureBox控件中显示选择的图片
        /// <summary>
        /// 用于在PictureBox控件中显示选择的图片
        /// </summary>
        /// <param name="openF">图像名</param>
        /// <param name="MyImage">pictureBox控件ID</param>
        public void Read_Image(OpenFileDialog openF, PictureBox MyImage)//显示选择的图片
        {
            //指定OpenFileDialog控件打开的文件格式
            openF.Filter = "*.jpg|*.jpg|*.bmp|*.bmp";
            if (openF.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    //将图片文件存入到PictureBox控件中
                    MyImage.Image = System.Drawing.Image.FromFile(openF.FileName);
                }
                catch (Exception)
                {
                    //弹出错误信息
                    MessageBox.Show("您选择的图片不能被读取或文件类型不对!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    throw;
                }
            }
        }

        #endregion

        #region 用于将图片以二进制形式存入数据库中
        /// <summary>
        /// 用于将图片以二进制形式存入数据库中
        /// </summary>
        /// <param name="FilmID">影片ID</param>
        /// <param name="openF"></param>
        public void SaveImage(string FilmID, OpenFileDialog openF)//将图片以二进制存入数据库中
        {
            string strimg = openF.FileName.ToString();  //记录图片的所在路径
            FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
            byte[] imgBytesIn = br.ReadBytes((int)fs.Length);//将流读入到字节数组中
            SqlConnection conn = sqlhelper.getcon();
            conn.Open();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Film Set F_FPhoto=@Photo where F_FId=" + FilmID);
            SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
            cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = http://www.mamicode.com/imgBytesIn;
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        #endregion

        #region 用于将图片从数据库中取出并显示在PictureBox控件中
        /// <summary>
        /// 用于将图片从数据库中取出并显示在PictureBox控件中
        /// </summary>
        /// <param name="FilmID">影片ID</param>
        /// <param name="pb">PictureBox控件ID</param>
        public void Get_Image(string FilmID, PictureBox pb)//将图片从数据库中取出
        {
            byte[] imagebytes = null;
            SqlConnection conn = sqlhelper.getcon();
            conn.Open();
            SqlCommand com = new SqlCommand("select * from T_Film where F_FId=‘" + FilmID + "‘", conn);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                imagebytes = (byte[])dr.GetValue(10);
            }
            dr.Close();
            conn.Close();
            MemoryStream ms = new MemoryStream(imagebytes);
            Bitmap bmpt = new Bitmap(ms);
            pb.Image = bmpt;
        }        
        #endregion

上面三个分别为公共类,第一个为打开对话框,第二个为将图像以二进制存入数据库,第三个为从数据库中读取出来。

在窗体代码中, Mymenu.Get_Image(FilmID, picboxPhoto);直接用就可以了(Mymenu)为公共类空间名称

C#_图片存取数据库Winform