zoukankan      html  css  js  c++  java
  • sqlserver数据库存取图片

       public void SaveImage(string MID, 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);  //将流读入到字节数组中
                conn.Open();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update tb_employee Set employeePhoto=@Photo where employeeID=" + MID);
                SqlCommand cmd = new SqlCommand(strSql.ToString(), conn);
                cmd.Parameters.Add("@Photo", SqlDbType.Binary).Value = imgBytesIn;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            public void Get_Image(string ygname,PictureBox pb)//将图片从数据库中取出
            {
                byte[] imagebytes = null;
                conn.Open();
                SqlCommand com = new SqlCommand("select * from tb_employee where employeeID='" + ygname + "'", conn);
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    imagebytes = (byte[])dr.GetValue(11);
                }
                dr.Close();
                conn.Close();
                MemoryStream ms = new MemoryStream(imagebytes);
                Bitmap bmpt = new Bitmap(ms);
                pb.Image = bmpt;
            }

    public void Read_Image(OpenFileDialog openF, PictureBox MyImage)  //显示选择的图片
            {
                openF.Filter = "*.jpg|*.jpg|*.bmp|*.bmp";   //指定OpenFileDialog控件打开的文件格式
                if (openF.ShowDialog() == DialogResult.OK)  //如果打开了图片文件
                {
                    try
                    {
                        MyImage.Image = System.Drawing.Image.FromFile(openF.FileName);  //将图片文件存入到PictureBox控件中
                    }
                    catch
                    {
                        MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }

    改进:

       /// <summary>         /// 从数据库中取出图片;         /// 参数:“控件”,“图片大小”,“sql相关参数”         /// </summary>         /// <param name="pbox"></param>         public void GetImage(PictureBox pbox, Size imageSize, string sql, SqlParameter[] sp, int columnIndex)         {             byte[] imageByte = null;             SqlDataReader reader = this.ExecuteReader(sql, sp);             while (reader.Read())             {                 imageByte = (byte[])reader.GetValue(columnIndex);             }             reader.Close();             MemoryStream ms = new MemoryStream(imageByte);             // Bitmap bmpt = new Bitmap(ms);                    //  pbox.Image = bmpt;//图片绑定到控件

                Bitmap bm = (Bitmap)Image.FromStream(ms);             ms.Close();

                Bitmap bp = new Bitmap(bm, imageSize);//将指定图片缩放到指定大小             pbox.Width = imageSize.Width;             pbox.Height = imageSize.Height;             pbox.Image = bp;//图片绑定到控件         }

  • 相关阅读:
    LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
    LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)
    LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
    LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
    LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
    LeetCode 617. 合并二叉树(Merge Two Binary Trees)
    LeetCode 206. 反转链表(Reverse Linked List) 16
    LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
    LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
    LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
  • 原文地址:https://www.cnblogs.com/xiaowei-blog/p/4174094.html
Copyright © 2011-2022 走看看