C# How to convert MessageBodyStream to MemoryStream?
通过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?的更多相关文章
- csharp:Convert Image to Base64 String and Base64 String to Image
/// <summary> /// 图像转成二进制数组 /// </summary> /// <param name="imageIn">< ...
- WPF 微信 MVVM 【续】修复部分用户无法获取列表
看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...
- C# base 64图片编码解码
使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...
- C# base64 Img 互转
[AcceptVerbs(HttpVerbs.Post)] public JsonResult Upload(HttpPostedFileBase fileData) { try { if (file ...
- canvas生成图片并保存到本地文件夹主要代码
js var url = canvas.toDataURL();//把canvas中的图片变成data:image C# string filepath = ""; string ...
- base64和图片的转换
/// <summary> /// base64转图片 /// </summary> /// <param name="strBase64">& ...
- UWP/Win10新特性系列—Drag&Drop 拖动打开文件
在Win10 App开发中,微软新增了系统PC文件与UWP 之间的文件拖拽行为,它支持将系统磁盘上的文件以拖拽的形式拖入App中并处理,在前不久的微软build 2015开发者大会上微软展示的UWP版 ...
- 中转Http请求
应用场景:公司与外部公司数据对接,外部公司需申请指定IP访问.而本地ip经常变动,无法因ip变动时刻向外部公司申请绑定IP,给本地程序调试带来麻烦,故只能在指定ip服务器上搭建请求中转http请求: ...
- 使用html2canvas实现批量生成条形码
/*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...
随机推荐
- VTK初始化New返回Null问题
原文链接:http://www.cppblog.com/mythma/archive/2013/08/02/vtk-6-new-null.html 在使用VTK6.0时候,会遇到X::New()返回为 ...
- Eclipse 插件ibeetl
启动Eclipse 打开菜单栏按一下菜单路径依次打开 Help -> Install New Softwave… ->点击Add按钮弹出一个对话框 弹出的对话框中Name随意填写,如填写“ ...
- sql server 查询数据判断为空
and xxx is NOT null and xxx is null
- 25-Ubuntu-文件和目录命令-其他命令-重定向
重定向 Linux允许将命令执行结果重定向到一个文件. 将本应显示到终端上的内容输出或追加到指定文件中. 重定向命令 含义 > 表示输出,会覆盖原有文件. >> 表示追加,会将内容追 ...
- PAT_A1147#Heaps
Source: PAT A1147 Heaps (30 分) Description: In computer science, a heap is a specialized tree-based ...
- css实现面包屑导航
HTML代码: <div id="breadcrumb"> <ul class="crumbs"> <li class=" ...
- Docker创建Mysql容器并通过命令行连接到容器
拉取网易蜂巢的mysql-server:5.6 docker pull hub.c.163.com/nce2/mysql:5.6 创建mysql5.6容器 1master+3个slave docker ...
- js中window.location的用法
用window.location处理解析当前页面URL window.location 对象所包含的属性 属性 描述 hash 从井号(#)开始的URL(锚点) host 主机名和当前URL的端口号 ...
- adchos 文本混淆工具
#-*- coding:utf-8 -*- import jieba import random import codecs import sys import string import chard ...
- NYIST 99 单词拼接
单词拼接时间限制:3000 ms | 内存限制:65535 KB难度:5 描述给你一些单词,请你判断能否把它们首尾串起来串成一串.前一个单词的结尾应该与下一个单词的道字母相同.如 aloha dog ...