C# winfrom 存取图片到数据库(二进制,image)
1.读取本地图片到PictureBox
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 转换成二进制存入数据库
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("失败!"); }
注:添加方法在上面
4.读取二进制转换成图片
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;
界面图
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2016-03-22 winform treeview绑定数据 DOM操作