各位乡亲父老,欢迎大家来捧场!江湖卖艺,生活不易!技艺交流(投稿、打广告、链接交换),请搓这里

  C#将相片转换成二进制存储在数据库中,再从数据库中显示出来

2019/11/21 19:00:50管理员 1547
- N +
#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)为公共类空间名称
0人赞 分享 二维码 赏一个
选择分享方式
移步手机端
文章手机二维码

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
选择打赏方式
微信赞助

打赏