1.讀取本地圖片到PictureBoxsql
public void InageShow(PictureBox PB) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Title = " 請選擇客戶端longin的圖片"; openfile.Filter = "Login圖片 (*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*"; if (DialogResult.OK == openfile.ShowDialog()) { try { Bitmap bmp = new Bitmap(openfile.FileName); pictureBox1.Image = bmp; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //字面是對當前圖片進行了二進制轉換 MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); //直接返這個值放到數據就好了 string ee = Convert.ToBase64String(arr); } catch { } } }
2.根據圖片路徑將本地圖片存入數據庫數據庫
private void button2_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 [PointSchool].[dbo].[Table_Image] ([image]) values (@image)"); int count = Write(strSql, imageBytes); if (count > 0) { MessageBox.Show("成功!"); } else { MessageBox.Show("失敗!"); } } }
private int Write(string strSql, byte[] imageBytes) { string connStr = "server=.;database=PointSchool;User =sa; pwd =123"; 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; } } } }
3.picturebox的Image 轉換成二進制存入數據庫spa
public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto) { //將Image轉換成流數據,並保存爲byte[] MemoryStream mstream = new MemoryStream(); imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp); byte[] byData = new Byte[mstream.Length]; mstream.Position = 0; mstream.Read(byData, 0, byData.Length); mstream.Close(); return byData; }
string strSql = string.Format("insert into [PointSchool].[dbo].[Table_Image] ([image]) values (@image)"); int count = Write(strSql, BBBB); if (count > 0) { MessageBox.Show("成功!"); } else { MessageBox.Show("失敗!"); }
注:添加方法在上面code
4.讀取二進制轉換成圖片orm
public void PicboxShow(PictureBox pictureBox2) { byte[] imagebytes = null; //打開數據庫 SqlConnection con = new SqlConnection("server=.;database=PointSchool;User =sa; pwd =123"); con.Open(); SqlCommand com = new SqlCommand("select top 1* from Table_Image", con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read()) { imagebytes = (byte[])dr.GetValue(1); } dr.Close(); com.Clone(); con.Close(); MemoryStream ms = new MemoryStream(imagebytes); Bitmap bmpt = new Bitmap(ms); pictureBox2.Image = bmpt; } pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
界面圖server