您的位置:首页 > 数据库

windows phone 开发中sql数据库 wcf服务与windows phone 8 之间图片二进制流存储与读取的问题

2014-05-25 22:07 543 查看
在做网站的时候,我们通常把图片文件以文件路径的形式存储在数据库。但是做移动开发的时候,这种方式是不可取的,你想想,别人下载了你的软件后,下载的时你的文件路径。或者你如果把图片之间存储在移动终端,那将占多大的内存----一个占内存的流氓软件。 即使用户愿意下载大量的图片。如果你的服务器更新了,那么用户读取的将只是图片的路径,出来不了图片的。

所以做移动开发的时候,一定要把图片通过二进制流的形式存储在数据库。

我们今天讲的是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;

}

这里注意一点,在模拟机上测试是无效的。要在真机上测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐