标签:obj toolstrip new var 路径 c# tostring 图片路径 from
方法1:
将图片复制到指定文件夹,在数据库中存储图片路径,通过读取路径来显示图片。
string str; private void toolStripButton1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { str = openFileDialog1.FileName; pictureBox1.Image = Image.FromFile(str); } }//打开文件并在PictureBox中显示图片 private void toolStripButton2_Click(object sender, EventArgs e) { string filename = DateTime.Now.Ticks.ToString();//时间戳,保证图片名称不重复 string name = @"F:\数据库作业\外卖\外卖\外卖\bin\Debug\image\" + filename; File.Copy(str, name);//将图片复制到指定文件夹 _users.Domain(name);//利用SQL将文件路径上传至数据库 } private void toolStripButton3_Click(object sender, EventArgs e) { var info = _users.SelectDomain();//SQL查询路径 pictureBox1.Image = Image.FromFile(info.Rows[0][0].ToString());//显示照片 this.pictureBox1.Refresh(); }
方法2:
将图片读成二进制后上传至数据库,再将二进制数据转化成图片。
string str; private void toolStripButton1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { str = openFileDialog1.FileName; pictureBox1.Image = Image.FromFile(str); } }//打开文件并在PictureBox中显示图片 private void toolStripButton2_Click(object sender, EventArgs e) { FileStream fs = new FileStream(str, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));//转成二进制数据的操作 _users.Domain(imgBytesIn); } private void toolStripButton3_Click(object sender, EventArgs e) { var table = _users.SelectDomain(); byte[] imagedata = (byte[])(table.Rows[0][0]); MemoryStream myStream = new MemoryStream(imagedata);//转成图片 pictureBox1.Image = Image.FromStream(myStream);//显示图片 } }
图片自适应picturebox用Sizemode-Stretchimage(PictureBox属性修改)。
标签:obj toolstrip new var 路径 c# tostring 图片路径 from
原文地址:http://www.cnblogs.com/2HBCCC/p/6959249.html