添加图片
//添加图片按钮 private void stgl_xz_but_tupian_Click(object sender, EventArgs e) { tianjiatupian.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";//定义可以打开的文件类型 DialogResult dia = tianjiatupian.ShowDialog(); if (dia == DialogResult.OK) { string filename = tianjiatupian.FileName; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);//将图片读入流中 st.Imgbytes= new byte[fs.Length];//二进制数组,用以临时存储图像的二进制编码 BinaryReader br = new BinaryReader(fs);//二进制读取器 st.Imgbytes = br.ReadBytes(Convert.ToInt32(fs.Length));//将图片读入到二进制数组中 //在图片框中显示图片 MemoryStream ms = new MemoryStream(st.Imgbytes, 0, st.Imgbytes.Length); ms.Write(st.Imgbytes, 0, st.Imgbytes.Length); Image img = Image.FromStream(ms); this.stgl_xz_tupian.SizeMode = PictureBoxSizeMode.Zoom;//设置自动适应大小 this.stgl_xz_tupian.Image = img; } } //清除图片按钮 private void stgl_xz_but_qingchu_Click(object sender, EventArgs e) { st.Imgbytes = null; this.stgl_xz_tupian.Image = null; }
//鼠标单击列表显示图片 private void stgl_view_MouseClick(object sender, MouseEventArgs e) { this.tupianyulan.Image = null; //将图像写入内存 if (stgl_view1.SelectedCells[2].Value.ToString() != "") { byte[] imgbytes = (byte[])stgl_view1.SelectedCells[2].Value; MemoryStream ms = new MemoryStream(imgbytes, 0, imgbytes.Length); ms.Write(imgbytes, 0, imgbytes.Length); Image img = Image.FromStream(ms); this.tupianyulan.SizeMode = PictureBoxSizeMode.Zoom;//设置自动适应大小 this.tupianyulan.Image = img; } }