http://www.eggheadcafe.com/articles/20040721.asp
http://www.vckbase.com/document/viewdoc/?id=1239
“Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。”
最近我们碰到一些论坛贴子,其问题大概都是围绕着“如何通过 Web 服务(WebService)序列化和发送一幅图像”以及“BinaryFormatter 不 工作——出现编程序集版本异常”这样的话题。我觉得现在是时候用仔细斟酌过的例子代码来说明问题,而不是不断地重复回帖。本文中我创建了一个简单的 Web 服务,其中包含了两个方法:
http://www.vckbase.com/document/viewdoc/?id=1239
“Web 的语义不是一种分离,而是当前 Web 的延伸,信息在其中被赋予了定义良好的内涵,使计算机和人之间能更好地协同工作。”
——Sir Tim Berners-Lee
最近我们碰到一些论坛贴子,其问题大概都是围绕着“如何通过 Web 服务(WebService)序列化和发送一幅图像”以及“BinaryFormatter 不 工作——出现编程序集版本异常”这样的话题。我觉得现在是时候用仔细斟酌过的例子代码来说明问题,而不是不断地重复回帖。本文中我创建了一个简单的 Web 服务,其中包含了两个方法:
- Getimage —— 它有一个字符串参数,用于接收图像文件名(采用 Server.MapPath),将图像从文件系统读进字节数组,然后 将该图像保存在一个简单的可序列化的类 ImageClas 的 public 类型字节数组属性“myImage”中。这个类就是该方法的返回类型。
- GetimageBytes —— 这个方法除了使用 BinaryFormatter 序列化 ImageClass 实例 并将得到的字节数组结果发送给调用者之外,其所做的事情与第一个方法完全一样。
首先把我们的图像存储在一个类实例属性中并序列化完整的类具有显著优点,这样做我们至少除了图象本身之外,我们还可以在网络上序列化和发送 附加的信息。
上述两个 WebMothods 方法的实现细节如下:
[WebMethod]我们必须把 ImageClass 类放在一个由 WebServices 类(即调用者)引用的单独类库工程中,,这样我们便可以防止 BinaryFormatter “程序集”错误,因为当我们试图反序列化 Formatter 输出时,程序集名称,版本和文化信息都将会匹配。如果你试图将 ImageClass 类放在实际的 Web 服务项目中,那么将会给你带来本可以避免的令人头痛的问题。解决此种问题的方法同样适用远程接口——即必须把 可序列化的“东西或内容”放到某个单独的类库中。下面是 ImageClass 类的代码 :
public byte[] GetimageBytes(string strImageName)
{
ImageClass ic=new ImageClass();
FileStream fs = new FileStream(Server.MapPath(strImageName),
FileMode.OpenorCreate,FileAccess.Read);
Byte[] img= new Byte[fs.Length ];
try
{
fs.Read(img,Convert.ToInt32(fs.Length));
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message+ex.StackTrace);
}
fs.Close();
ic.myImage=img;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms,ic);
return ms.ToArray();
}
[WebMethod]
public ImageClass Getimage(string strImageName)
{
ImageClass ic=new ImageClass();
FileStream fs = new FileStream(Server.MapPath(strImageName),FileAccess.Read);
Byte[] img= new Byte[fs.Length ];
try
{
fs.Read(img,Convert.ToInt32(fs.Length));
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message+ex.StackTrace);
}
fs.Close();
ic.myImage=img;
return ic;
}
using System;为了测试我们创建的 Web 服务,下面拟使用一个 Windows 窗体应用程序,此应用程序将使用一个 WebReference 来引用我们的 Web 服务,有两个按钮,每个按钮执行各自的方法,下面是每个按钮的各自代码:
using System.Reflection;
using System.Drawing;
namespace ImageClassAssy
{
[Serializable]
public class ImageClass
{
public byte[] myImage;
public ImageClass()
{
}
}
}
private void button1_Click(object sender,System.EventArgs e){ // Getimage Method BinaryFormatterSvc.Service1 s= new BinaryFormatterSvc.Service1(); BinaryFormatterSvc.ImageClass icc=s.Getimage(this.textBox1.Text); byte[] byt=icc.myImage; MemoryStream ms = new MemoryStream(byt); Bitmap b=(Bitmap) Image.FromStream(ms); pictureBox1.Image=b; }private void button2_Click(object sender,System.EventArgs e){ //GetimageBytes (BinaryFormatter) method BinaryFormatterSvc.Service1 s= new BinaryFormatterSvc.Service1(); byte[] byt=s.GetimageBytes(this.textBox1.Text); MemoryStream ms = new MemoryStream(byt); BinaryFormatter bf= new BinaryFormatter(); ImageClass ic=(ImageClass)bf.Deserialize(ms); MemoryStream ms2 = new MemoryStream(ic.myImage); Bitmap b=(Bitmap) Image.FromStream(ms2); pictureBox1.Image=b;}下面是方法调用后的结果,图片是我信手拿来的一张照片——在服务器上的一张名为“Pete.jpg”的图片:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。