建立一个windows窗体应用程序,在form1界面中拖入两个按钮和一个pictureBox,通过输入输出流来上传图片和显示图片。需要添加一下openFileDialog1.

界面如下:
02-20 winform 上传图片并读取图片

在cs中写上传和显示图片的方法

 1  //上传图片
 2         private void button1_Click(object sender, EventArgs e)
 3         {
 4             //图片的转化
 5             openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";//设置图片另存为文件类型格式,filter是文件筛选器
 6             DialogResult da = openFileDialog1.ShowDialog();
 7             if(da==DialogResult.OK)
 8             {
 9                 string fil = openFileDialog1.FileName;
10                 FileStream fs = new FileStream(fil,FileMode.Open,FileAccess.Read);
11                 byte[] img=new byte[fs.Length];
12                 BinaryReader br = new BinaryReader(fs);//二进制读取器
13                 img = br.ReadBytes(Convert.ToInt32(fs.Length));
14                 //链接数据库
15                SqlConnection conn = new SqlConnection("server=.;database=newData;user=sa;pwd=123");
16                conn.Open();
17                SqlCommand cmd = conn.CreateCommand();
18                cmd.CommandText = "insert into Table_1 values(@image)";
19                cmd.Parameters.Clear();
20                cmd.Parameters.Add("@image",img);
21                cmd.ExecuteNonQuery();
22                conn.Close();
23                MessageBox.Show("上传成功");
24 
25             }
上传图片的方法

相关文章:

  • 2022-02-27
  • 2021-08-31
  • 2021-07-16
  • 2022-12-23
  • 2021-11-07
  • 2021-05-21
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-02
  • 2021-12-27
  • 2021-12-31
  • 2021-12-27
  • 2022-02-11
相关资源
相似解决方案