通过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. HDU_5724_状态压缩的sg函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5724 题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放 ...

  2. js 验证文件格式和大小

    <script> $('#btnSearch').click(function(){ // alert("000");// fileElem = document.ge ...

  3. html 复杂表格

    123456789 123456789 0000000000 日期 123456789 1234560000000789 ----------- ----------- ----------- --- ...

  4. 分布式锁获取token

    package com.sankuai.qcs.regulation.nanjing.util; import com.dianping.squirrel.client.StoreKey; impor ...

  5. eas之辅助编辑功能

    copy.cut.paste// copytable.getEditHelper().copy();// cuttable.getEditHelper().cut();// pastetable.ge ...

  6. HDU-1134 卡特兰数+java大数模板

    题意: 给你一个n,然后1,2,3...2n-1,2n围一圈,让每个数都能用一条线配对并且线与线之间不能交叉,问有几种方法数. 思路: 1 可以和2,4,6...连接.假如   一共有8个数,1和2连 ...

  7. [poj1325] Machine Schedule (二分图最小点覆盖)

    传送门 Description As we all know, machine scheduling is a very classical problem in computer science a ...

  8. Qt5.11+opencv3.4的配置安装

    系统:Windows 10 64位 前期准备: 1.CMake下载安装 下载地址:https://cmake.org/download/ 选择msi安装文件,按照提示一步一步按照就好 可以参考:htt ...

  9. docker安装部署

    1. 如何安装 Epel源到 RHEL/CentOS 7/6/5? RHEL/CentOS rpm -ivh http://mirrors.ustc.edu.cn/epel/7/x86_64/Pack ...

  10. 继续聊WPF——Expander控件(1)

    这个控件最实用的地方,就是做导航栏. <StackPanel Margin="20,20" Width="100" Height="460&qu ...