windows phone 开发中sql数据库 wcf服务与windows phone 8 之间图片二进制流存储与读取的问题
2014-05-25 22:07
543 查看
在做网站的时候,我们通常把图片文件以文件路径的形式存储在数据库。但是做移动开发的时候,这种方式是不可取的,你想想,别人下载了你的软件后,下载的时你的文件路径。或者你如果把图片之间存储在移动终端,那将占多大的内存----一个占内存的流氓软件。 即使用户愿意下载大量的图片。如果你的服务器更新了,那么用户读取的将只是图片的路径,出来不了图片的。
所以做移动开发的时候,一定要把图片通过二进制流的形式存储在数据库。
我们今天讲的是windows phone 8 通过wcf服务和sql数据库存储二进制流信息。
先说下wcf端的配置,在 <system.serviceModel>下添加<bind
修改<system.web>
配置如下
第一步:
首先你应该通过选择器获取数据流。
点击上传按钮
//第二部,在wcf端接收wp端传进来的GoodName, GoodPrice, StudentName, GoodType, GoodDetail, GoodPhoto, DateTime, GPhone字段,将所有字段存入数据库
//根据wp 传过来的流,将流存入数据库
//到此整个存储数据成功,那我们现在把图片读取出来啦
//在wp端,我们传进来StudentName
在wcf端从数据库中读取二进制流并返回调用他的wp端
//根据传过来的用户名,查找是用的头像,返回头像的字符串
public byte[] returnPhoto(string StudentName)
{
byte[] StudentPhoto = null;
DBHelper db = new DBHelper();
SqlConnection cnn = db.ConnectionCnnString1;
string sql = "select StudentPhoto from Student where StudentName='" + StudentName + "'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = sql;
byte[] bt = (byte[])cmd.ExecuteScalar();
cnn.Close();
return bt;
}
这里注意一点,在模拟机上测试是无效的。要在真机上测试。
所以做移动开发的时候,一定要把图片通过二进制流的形式存储在数据库。
我们今天讲的是windows phone 8 通过wcf服务和sql数据库存储二进制流信息。
先说下wcf端的配置,在 <system.serviceModel>下添加<bind
<bindings> <!--<wsHttpBinding>--> <basicHttpBinding> <binding sendTimeout="00:10:00" transferMode="Streamed" messageEncoding="Text" textEncoding="utf-8" maxReceivedMessageSize="9223372036854775807" maxBufferPoolSize="2147483647" > <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> </binding> </basicHttpBinding> <!--</wsHttpBinding>--> </bindings>
修改<system.web>
配置如下
<system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" maxRequestLength="102400"/> </system.web>
第一步:
首先你应该通过选择器获取数据流。
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; //引入命名空间 using System.Windows.Media.Imaging; using Microsoft.Phone.Tasks; using System.IO; { public partial class UpGood : PhoneApplicationPage { byte[] GoodPhoto; //相机选择器 CameraCaptureTask MyCamera = new CameraCaptureTask(); //本地照片选择器 PhotoChooserTask ptc = new PhotoChooserTask(); public UpGood() { InitializeComponent(); // 第二步,在页面构造函数中注册完成回调事件 MyCamera.Completed += new EventHandler<PhotoResult>(MyCamera_Completed); ptc.Completed += new EventHandler<PhotoResult>(ptc_Completed); } private void btnCamera_Click(object sender, RoutedEventArgs e) { // 第三步,显示组件 MyCamera.Show(); } // 第四步,处理事返回结果 public void MyCamera_Completed(object sender, PhotoResult e) { //把事件处理结果的Stream转化成二进制 byte[] GoodPhoto = new byte[e.ChosenPhoto.Length]; e.ChosenPhoto.Read(GoodPhoto, 0, (int)e.ChosenPhoto.Length); //在客户端及时显示选择器选择的图片 if (e.TaskResult == TaskResult.OK) { // 从返回的流中创建图象 Stream stream=e.ChosenPhoto; BitmapImage bmp = new BitmapImage(); try { bmp.SetSource(e.ChosenPhoto); // 把图象作为Image控件的源。 // 防止异步回调直接访问UI元素,故应使用BeginInvoke方法。 Dispatcher.BeginInvoke(() => { this.img.Source = bmp; }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } //处理选择本地照片 void ptc_Completed(object sender, PhotoResult e) { byte[] GoodPhoto = new byte[e.ChosenPhoto.Length]; e.ChosenPhoto.Read(GoodPhoto, 0, (int)e.ChosenPhoto.Length); if (e.TaskResult == TaskResult.OK) { BitmapImage bmp = new BitmapImage(); try { bmp.SetSource(e.ChosenPhoto); Dispatcher.BeginInvoke(() => { this.img.Source = bmp; }); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } private void btnShow_Click(object sender, RoutedEventArgs e) { ptc.Show(); }
点击上传按钮
<span style="font-size:14px;"> private void UpGoods_Click(object sender, EventArgs e) { String GoodName=txtGoodName.Text.Tostring(); //将信息传到数据库 ServiceReference1.Service1Client saveGood = new ServiceReference1.Service1Client(); saveGood.SaveGoodInfoCompleted += new EventHandler<ServiceReference1.SaveGoodInfoCompletedEventArgs>(saveGood_SaveGoodInfoCompleted); saveGood.SaveGoodInfoAsync(GoodName,GoodPhoto ); } private void saveGood_SaveGoodInfoCompleted(object sender, ServiceReference1.SaveGoodInfoCompletedEventArgs e) { if (e.Result == true) { MessageBox.Show("添加成功"); } else { MessageBox.Show("添加失败!"); } } } }</span>
//第二部,在wcf端接收wp端传进来的GoodName, GoodPrice, StudentName, GoodType, GoodDetail, GoodPhoto, DateTime, GPhone字段,将所有字段存入数据库
//根据wp 传过来的流,将流存入数据库
<span style="font-size:14px;"> public bool SaveGoodInfo(string GoodName, byte[] GoodPhoto) { DBHelper db = new DBHelper(); SqlConnection cnn = db.ConnectionCnnString1; string sql = "insert into Goods(GoodName ,GoodPhoto ) values('" + GoodName + "','" + GoodPhoto + "')"; int i = db.AddTable(sql); if (i == 1) { //插入成功数据 return true; } else { //插入失败 return false; } </span> }
//到此整个存储数据成功,那我们现在把图片读取出来啦
//在wp端,我们传进来StudentName
<span style="font-size:14px;">protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); string StudentName =txtStudentName.Text.Tostring(); //这里还要根据用户的名字 ,查找用户的头像 ServiceReference1.Service1Client Stuph = new ServiceReference1.Service1Client(); Stuph.returnPhotoCompleted+= new EventHandler<ServiceReference1.returnPhotoCompletedEventArgs> (Stuph_returnPhotoCompleted); Stuph.returnPhotoAsync(StudentName); } } private void Stuph_returnPhotoCompleted(object sender,ServiceReference1.returnPhotoCompletedEventArgs e) { //这里根据从wcf端返回的二进制流信息,生成一张图片,在前台的Image控件上显示。这里主意要 //添加 // using System.Windows.Media.Imaging; // using System.IO; 引用 MemoryStream ms = new MemoryStream(e.Result); BitmapImage bi = new BitmapImage(); bi.SetSource(ms); image.Source = bi; }</span>
在wcf端从数据库中读取二进制流并返回调用他的wp端
//根据传过来的用户名,查找是用的头像,返回头像的字符串
public byte[] returnPhoto(string StudentName)
{
byte[] StudentPhoto = null;
DBHelper db = new DBHelper();
SqlConnection cnn = db.ConnectionCnnString1;
string sql = "select StudentPhoto from Student where StudentName='" + StudentName + "'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = sql;
byte[] bt = (byte[])cmd.ExecuteScalar();
cnn.Close();
return bt;
}
这里注意一点,在模拟机上测试是无效的。要在真机上测试。
相关文章推荐
- C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。
- Windows Phone 7 开发 31 日谈——第21日:Silverlight Toolkit for Windows Phone
- Windows phone 应用开发[7]-MEF For Windows phone
- Windows Phone 7 开发 31 日谈——第21日:Silverlight Toolkit for Windows Phone
- Unity3d开发android项目时引入jar包读取图片资源的问题
- Windows phone手机开发之图片选择器,照相机功能,计时器,加速度,图片路径问题,页面间传值,随机产生数字
- Windows Phone 7 开发 31 日谈——第21日:Silverlight Toolkit for Windows Phone
- Windows Phone 开发学习笔记(七) Hello Windows Phone之有始有终
- MSDN Webcast - Silverlight for Windows Phone 开发系列课程(1):Windows Phone平台概况
- Windows Phone实用开发技巧(39):WebBrowser引用独立存储空间中的图片
- Windows Phone Studio-任何人都能开发Windows Phone App的在线工具
- 解决Java Web开发中Jsp存储读取MySQL数据中文乱码的问题
- Windows Phone 开发学习笔记(二) Hello Windows Phone之不费功夫
- MSDN Webcast - Silverlight for Windows Phone 开发系列课程(2):Windows Phone开发环境搭建
- Windows Phone 7 开发 31 日谈 (31 Days of Windows Phone 7)
- Windows phone 应用开发[7]-MEF For Windows phone
- Windows Phone 开发学习笔记(四) Hello Windows Phone之心有灵犀
- Windows Phone 开发(一):入门指南 — 安装开发环境:Windows Phone SDK
- .Net MVC4 使用心得(一)使用uploadify和wcf存储服务时的文件上传问题
- 电子商务文件存储及读取服务的设计和开发思路