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 ...
随机推荐
- Linux各发行版配置总结
Ubuntu 14.04(该版本下亲测好用) 1.修改开机启动项和默认启动项 sudo gedit /boot/grub/grub.cfg(修改前最好备份一个) 2.登陆界面图片的修改 /usr/sh ...
- layui 下拉框取值
layui.use('form', function () { var form = layui.form; form.on('select(Status)', function (data) { c ...
- 微信jssdk图片上传
一.html页面如下: <div class="weui-cell"> <div class="weui-cell__hd"></ ...
- Java-Class-Test:Test-1
ylbtech-Java-Class-Test:Test-1 1.返回顶部 1.1. package com.ylbtech.api; import com.y;btech.WxApiApplicat ...
- python学习笔记之小小购物车
#coding=utf-8 ''' Created on 2015-6-18 @author: 悦文 ''' def goods_list(): shangpin={"} print &qu ...
- res对象json,redirect
1.res.json() var express=require('express'); var app=express(); app.get('/',function(req,res){ //返回j ...
- 获取url
首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...
- 多重循环、缓冲区管理、数组(day06)
无法预知的数字叫随机数 rand标准函数可以用来获得随机数 为了使用这个标准函数需要包含stdlib.h头文件 srand标准函数用来设置随机数种子 这个函数把一个整数作为种子使用 不同的种子产生的随 ...
- 《团队名称》第八次团队作业:Alpha冲刺
项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 代码敲不队 作业学习目标 (1)掌握软件测试基础技术(2)学习迭代式增量软件开发过程(Scrum) ...
- 修改layui的后台模板的左侧导航栏可以伸缩
原生的左侧导航栏代码: <div class="layui-side layui-bg-black"> <div class="layui-side-s ...