图片保存至数据库的方法
2016-04-29 11:40
369 查看
1:为控件绑定图片
BitmapImage bitmapImage; bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = System.IO.File.OpenRead(@"E:\2.jpg"); bitmapImage.EndInit(); image.Source = bitmapImage;//image是XAML页面上定义的Image控件2:把图片保存至imageData数组中并保存至数据库中
byte[] imageData = new byte[bitmapImage.StreamSource.Length]; // now, you have get the image bytes array, and you can store it to SQl Server bitmapImage.StreamSource.Seek(0, System.IO.SeekOrigin.Begin); //very important, it should be set to the start of the streambitmapImage.StreamSource.Read(imageData, 0, imageData.Length); 3:从数据库中读取数据保存为字节组,显示在页面上 System.IO.MemoryStream ms = new System.IO.MemoryStream(imageData);//imageData是从数据库中读取出来的字节数组ms.Seek(0, System.IO.SeekOrigin.Begin); BitmapImage newBitmapImage = new BitmapImage(); newBitmapImage.BeginInit(); newBitmapImage.StreamSource = ms; newBitmapImage.EndInit(); image2.Source = newBitmapImage;
相关文章推荐
- mysql查询优化技巧
- MVC自动生成数据库【Code-FIrst方式】
- MySQL5.7重置 root 密码
- SQLServer2008新建链接服务器for Oracle
- MySQL创建用户与授权方法
- mysql一主多从同步配置
- Mongodb 创建索引
- Redis --> 为redis分配新的端口
- nagios监控mongodb
- MySQL:索引工作原理及使用
- Redis常用命令速查 <第二篇>
- sql详解
- Redis介绍及常用命令
- 第7讲-SQL语言复杂查询--(NOT)Exists
- SQL Server 批量插入数据的两种方法
- redis 基本命令学习一(strings、hashes、lists)
- SQL架构信息读取
- 批量修改数据库中字段的数据类型
- mysql常用操作
- 大并发大数量中的MYSQL瓶颈与NOSQL介绍