c#儲存圖片至SQL資料庫
阿新 • • 發佈:2019-01-12
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 圖片 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Title = "請選擇客戶端longin的圖片"; openfile.Filter = "Login圖片(*.jpg;*.bmp;*png)|*.jpeg;*.jpg;*.bmp;*.png|AllFiles(*.*)|*.*"; if (DialogResult.OK == openfile.ShowDialog()) { try { //讀成二進位制 byte[] bytes = File.ReadAllBytes(openfile.FileName); //直接返這個儲存到資料就行了cmd.Parameters.Add("@image", SqlDbType.Image).Value = bytes; //輸出二進位制 在這裡把資料中取到的值放在這裡byte[] bytes=(byte[])model.image; pictureBox1.Image = System.Drawing.Image.FromStream(new MemoryStream(bytes)); this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; // 如果儲存成檔案: //File.WriteAllBytes(@"d:\text.jpg", bytes); } catch { } } } } }
放一個picbox和button。