zoukankan      html  css  js  c++  java
  • C# winfrom 存取图片到数据库(二进制,image)

    1.读取本地图片到PictureBox

     public void InageShow(PictureBox PB)
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Title = " 请选择客户端longin的图片";
                openfile.Filter = "Login图片 (*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*";
                if (DialogResult.OK == openfile.ShowDialog())
                {
                    try
                    {
                        Bitmap bmp = new Bitmap(openfile.FileName);
                        pictureBox1.Image = bmp;
                        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
    
                       //字面是对当前图片进行了二进制转换
                        MemoryStream ms = new MemoryStream();
                        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                        byte[] arr = new byte[ms.Length];
                        ms.Position = 0;
                        ms.Read(arr, 0, (int)ms.Length);
                        ms.Close();
                        //直接返这个值放到数据就行了
                       string ee = Convert.ToBase64String(arr);
                    }
                    catch { }
                }
            }

    2.根据图片路径将本地图片存入数据库

            private void button2_Click(object sender, EventArgs e)
            {
                //获取用户打开的路径然转换成二进制存入数据库
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
    
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string filePath = ofd.FileName;//图片路径
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    byte[] imageBytes = new byte[fs.Length];
                    BinaryReader br = new BinaryReader(fs);
                    imageBytes = br.ReadBytes(Convert.ToInt32(fs.Length));//图片转换成二进制流
    
                    string strSql = string.Format("insert into [PointSchool].[dbo].[Table_Image] ([image]) values (@image)");
                    int count = Write(strSql, imageBytes);
    
                    if (count > 0)
                    {
                        MessageBox.Show("成功!");
                    }
                    else
                    {
                        MessageBox.Show("失败!");
                    }
                }
    
            }
            private int Write(string strSql, byte[] imageBytes)
            {
                string connStr = "server=.;database=PointSchool;User =sa; pwd =123";
    
                using (SqlConnection conn = new SqlConnection(connStr))
                {
                    using (SqlCommand cmd = new SqlCommand(strSql, conn))
                    {
                        try
                        {
                            conn.Open();
                            SqlParameter sqlParameter = new SqlParameter("@image", SqlDbType.Image);
                            sqlParameter.Value = imageBytes;
                            cmd.Parameters.Add(sqlParameter);
                            int rows = cmd.ExecuteNonQuery();
                            return rows;
                        }
                        catch (Exception e)
                        {
                            throw;
                        }
                    }
                }
            }


    3.picturebox的Image 转换成二进制存入数据库

        public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
            {
                //将Image转换成流数据,并保存为byte[] 
                MemoryStream mstream = new MemoryStream();
                imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
                byte[] byData = new Byte[mstream.Length];
                mstream.Position = 0;
                mstream.Read(byData, 0, byData.Length);
                mstream.Close();
                return byData;
            }
      string strSql = string.Format("insert into [PointSchool].[dbo].[Table_Image] ([image]) values (@image)");
                int count = Write(strSql, BBBB);
                if (count > 0)
                {
                    MessageBox.Show("成功!");
                }
                else
                {
                    MessageBox.Show("失败!");
                }

    注:添加方法在上面

     4.读取二进制转换成图片

            public void PicboxShow(PictureBox pictureBox2)
            {
                byte[] imagebytes = null;
    
                //打开数据库
    
                SqlConnection con = new SqlConnection("server=.;database=PointSchool;User =sa; pwd =123");
    
                con.Open();
    
                SqlCommand com = new SqlCommand("select top 1* from Table_Image", con);
    
                SqlDataReader dr = com.ExecuteReader();
    
                while (dr.Read())
                {
    
                    imagebytes = (byte[])dr.GetValue(1);
    
                }
    
                dr.Close();
    
                com.Clone();
    
                con.Close();
    
                MemoryStream ms = new MemoryStream(imagebytes);
    
                Bitmap bmpt = new Bitmap(ms);
    
                pictureBox2.Image = bmpt;
    
            }
    
       pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;

    界面图

  • 相关阅读:
    undefined与null
    php中实现MVC的思想
    jquery返回json格式数据来获取每天的天气预报
    在php.ini中safe_mode开启之后对于PHP系统函数有什么影响呢?
    PHP的垃圾收集机制是怎样的
    javascript图片预加载技术
    web标准
    javascript简介
    javascript中的四种循环语句
    那些年,我还在学习C#
  • 原文地址:https://www.cnblogs.com/hanke123/p/6600178.html
Copyright © 2011-2022 走看看