最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

把图片保存到数据库中和从数据库中读取图片

来源:懂视网 责编:小采 时间:2020-11-09 16:03:53
文档

把图片保存到数据库中和从数据库中读取图片

把图片保存到数据库中和从数据库中读取图片:最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图
推荐度:
导读把图片保存到数据库中和从数据库中读取图片:最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图

最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图

  最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。

  1、将图片作为其中的一个参数保存到数据库中

  在项目中,一般是将图片转换成二进制流格式,然后保存到数据库中。同时数据库表中存储图片的格式一般为image。此次项目,是将图片作为一个参数,和其他几个参数一起保存到数据库中,和在网上搜索到的图片保存不太一样,此处稍作修改,但都是检测过的。

  存储步骤:

  1、搜索到图片的路径

  2、读取图片并将图片转换成二进制流格式

  3、sql语句保存到数据库中。

  贴代码: 

private void btnWrite_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 [SBS].[dbo].[Model] ([M_QRCode],[M_Skills] ) values (@image,'2')");
 int count = Write(strSql,imageBytes );

 if (count > 0)
 {
 MessageBox.Show("success");
 }
 else
 {
 MessageBox.Show("failed");
 }
 }
 }

  数据库连接和保存图片语句:

private int Write(string strSql,byte[] imageBytes)
 {
 string connStr = "Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;";

 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;
 }
 }
 }
 }

View Code

  2、从数据库总读取图片

  从数据库中读取图片字段,并转换成内存流生成bitmap。

  贴代码: 

private void btnRead_Click(object sender, EventArgs e)
 {
 string strSql = string.Format("select M_QRCode from [SBS].[dbo].[Model] where M_id = 7");//图片保存的字段是M_QRCode
 Read(strSql);
 }

 private void Read(string strSql)
 {
 string connStr = "Data Source=192.168.4.132;initial Catalog=SBS;User ID=sa;Password=sa;";

 using (SqlConnection conn = new SqlConnection(connStr))
 {
 using (SqlCommand cmd = new SqlCommand(strSql, conn))
 {
 conn.Open();
 SqlDataReader sqlDr = cmd.ExecuteReader();
 sqlDr.Read();
 byte[] images = (byte[])sqlDr["M_QRCode"];
 MemoryStream ms = new MemoryStream(images);
 Bitmap bmp = new Bitmap(ms);
 pictureBox1.Image = bmp;
 }
 }
 }

  3、根据图片路径显示图片

  这个比较简单,直接贴出代码 

private void btnLoad_Click(object sender, EventArgs e)
 {
 OpenFileDialog ofd = new OpenFileDialog();
 ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
 if (ofd.ShowDialog() == DialogResult.OK)
 {
 pictureBox1.Image = Image.FromFile(ofd.FileName);
 }
 }

  4、打印图片

  打印图片是在将图片显示在pictureBox的基础上进行的。

  步骤:

  1、将printDocument控件拖到界面,添加打印代码

  2、设置PrintDocument控件的Print_PrintPage事件

private void btnPrint_Click(object sender, EventArgs e)
 {
 PrintDialog printDialog = new PrintDialog();
 printDialog.Document = this.printDocument1;
 if (printDialog.ShowDialog() == DialogResult.OK)
 {
 try
 {
 printDocument1.Print();
 }
 catch (Exception ex)
 {
 printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());
 }
 }
 }

 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
 {
 e.Graphics.DrawImage(pictureBox1.Image, 30, 30);
 }

  

  附带着将图片转换成二进制和将二进制转换成图片专门写出来,以便于查看。 

 public byte[] ConvertBinary(string filePath)
 {
 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);//以文件流形式读取图片
 BinaryReader br = new BinaryReader(fs);//转换成二进制流
 byte[] imageBytes = br.ReadBytes((int)fs.Length);//保存到字节数组中

 return imageBytes;
 }

 public void ShowImage(byte[] imageBytes)
 {
 MemoryStream ms = new MemoryStream(imageBytes);
 pictureBox1.Image = Image.FromStream(ms);
 }

  在pictureBox中显示图片的三种方式: 

public void Method()
 {
 MemoryStream ms;
 pictureBox1.Image = Image.FromStream(ms);

 Bitmap bitmap;
 pictureBox1.Image = bitmap;

 string filePath;
 pictureBox1.Image = Image.FromFile(filePath);
 }

  winform中控件combobox控件使用: 

public void BindCombobox()
 {
 DataTable dt = new DataTable();
 dt.Columns.Add(new DataColumn("id", typeof(int)));
 dt.Columns.Add(new DataColumn("value", typeof(string)));

 for (int i = 0; i < 3; i++)
 {
 DataRow dr = dt.NewRow();
 dr["id"] = i;
 dr["value"] = 10 + i;
 dt.Rows.Add(dr);
 }

 this.comboBox1.DataSource = dt;
 this.comboBox1.DisplayMember = "value";
 this.comboBox1.ValueMember = "id"; 
 }

 public void ShowValue()
 {
 this.textBox1.Text = this.comboBox1.Text;
 this.textBox2.Text = this.comboBox1.SelectedValue.ToString();
 }

  

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

把图片保存到数据库中和从数据库中读取图片

把图片保存到数据库中和从数据库中读取图片:最近做到一个小项目,其中关系到图片的一些操作。比如:将图片保存到数据库中、从数据库中读取图片、显示图片、打印图片等。此处对这些在项目中遇到的一些琐碎知识加以总结,以便日后查找。 1、将图片作为其中的一个参数保存到数据库中 在项目中,一般是将图
推荐度:
标签: 保存 图片 一个
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top