通过WCF服务从数据库取文档数据时,返回的是Stream对象,在DevExpress的PDFViewer显示时,用PDFViewer.LoadDocunent(Stream stream);方法时,报无法从MessageBodyStream转为Stream 的错误。

    最后在StackOverFlow上找到了答案

不能通过转型,只能通过从Stream中读出数据,然后写入到MemoryStream的方法。

  

I am returning a Stream from a WCF Service and trying to convert it to a MemoryStream.But in the web application where in consume the WCF service,I am getting the result as of "MessageBodyStream" where i was expecting "System.IO.Stream". How can i convert this to a MemoryStream ?

     下面是采纳的回答:

    Sometimes the streams come in and you dont know how big they are, for that use this code:

public static byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position; byte[] readBuffer = new byte[]; int totalBytesRead = ;
int bytesRead; while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > )
{
totalBytesRead += bytesRead; if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -)
{
byte[] temp = new byte[readBuffer.Length * ];
Buffer.BlockCopy(readBuffer, , temp, , readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
} byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, , buffer, , totalBytesRead);
}
return buffer;
}

Then once you have the byte array you can convert it to a memory stream...

  byte[] myBytes = ReadToEnd(theStream);
Stream theMemStream = new MemoryStream(myBytes, , myBytes.Length);

C# How to convert MessageBodyStream to MemoryStream?的更多相关文章

  1. csharp:Convert Image to Base64 String and Base64 String to Image

    /// <summary> /// 图像转成二进制数组 /// </summary> /// <param name="imageIn">< ...

  2. WPF 微信 MVVM 【续】修复部分用户无法获取列表

    看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...

  3. C# base 64图片编码解码

    使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...

  4. C# base64 Img 互转

    [AcceptVerbs(HttpVerbs.Post)] public JsonResult Upload(HttpPostedFileBase fileData) { try { if (file ...

  5. canvas生成图片并保存到本地文件夹主要代码

    js var url = canvas.toDataURL();//把canvas中的图片变成data:image C# string filepath = ""; string ...

  6. base64和图片的转换

    /// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...

  7. UWP/Win10新特性系列—Drag&Drop 拖动打开文件

    在Win10 App开发中,微软新增了系统PC文件与UWP 之间的文件拖拽行为,它支持将系统磁盘上的文件以拖拽的形式拖入App中并处理,在前不久的微软build 2015开发者大会上微软展示的UWP版 ...

  8. 中转Http请求

    应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问.而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求: ...

  9. 使用html2canvas实现批量生成条形码

    /*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...

随机推荐

  1. Cell期刊论文:为什么计算机人脸识别注定超越人类?(祖母论与还原论之争)

    终于找到ML日报的微信链接,抄之...................................... 请拜访原文链接:[祖母论与还原论之争]为什么计算机人脸识别注定超越人类?评价:       ...

  2. jquery radio、 checkbox、 select 操作

    转载:http://www.haorooms.com/post/checkandselect $("input[id^='code']");//id属性以code开始的所有inpu ...

  3. 集合运算(UNION)

    表的加法 集合运算:就是满足统一规则的记录进行的加减等四则运算. 通过集合运算可以得到两张表中记录的集合或者公共记录的集合,又或者其中某张表中记录的集合. 集合运算符:用来进行集合的运算符. UNIO ...

  4. count(*)实现原理+两阶段提交总结

    count(*)实现原理 不同引擎的实现: MyISAM引擎把表的总行数存在了磁盘上,执行COUNT(*)就会直接返回,效率很高: InnoDB在count(*)时,需要把数据一行一行的从引擎里面取出 ...

  5. day004 与用户交互、格式化输出、基本运算符

    目录 今天Python所学习的知识如下:①与用户的交互.格式化输出.基本运算符.以下整理汇总下所学习的知识点. 与用户的交互 input 注意事项: input函数接受的都是字符串 python2中的 ...

  6. [adb]查看 App的appPackage和appActivity

    最近在写app的UI框架,写脚本之前需要知道app的包名和activity,如果获取呢: 需求配置abdrioid sdk环境 方法1:abd log 1. 打开cmd命令窗口2.在命令窗口中输入,a ...

  7. codeforces 244B-Undoubtedly Lucky Numbers 搜索

    题意:给你一个n,求不大于n的并且仅由两种或者一种数字组成的数的个数.(有点绕,,简单点就是,看看小于等于n点数中,,有多少数字只有一种数字,或者有两种数字组成) “哎,自己还是太菜了,训练的时候只做 ...

  8. 【例题4-2 uva489】Hangman Judge

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水题. 中间就赢了算赢.(重复说,算错 [代码] #include <bits/stdc++.h> using name ...

  9. 淘宝的开源分布式文件系统TFS

    TFS(Taobao FileSystem)是淘宝团队开源的海量非结构化数据存储设计的分布式系统.构筑在普通的Linux机器集群上,可为外部提供高可靠和高并发的存储访问.高可扩展.高可用.高性能.面向 ...

  10. 暑假集训D14总结

    %dalao 今天dalao继续来讲课~讲的是一个叫kd树的奇怪东西= = 然而啥都没听懂 考试 今天多校联考,日常炸= = 照例打了前两道题的暴力(T1随便hash一下就水过了啊喂),然后开始推T3 ...