zoukankan      html  css  js  c++  java
  • C#将相片转换成二进制存储在数据库中,再从数据库中显示出来

            #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 = 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)为公共类空间名称

  • 相关阅读:
    Webservice学习之新建一个最简单的Webservice项目
    初学程序一定要养成良好的习惯
    你晚上睡好了吗?
    如何面对失恋?
    多病之秋少言多饮
    转:避开秋季相冲食物
    转:饭后九不要包你保健康
    转:五官不适预示五脏衰弱
    转:过度疲劳的27个信号与预防方法
    foxmail是不是不行了?
  • 原文地址:https://www.cnblogs.com/jys509/p/2020258.html
Copyright © 2011-2022 走看看