如何将从httpwebresponse获取的Web流转换为位图,以及如何将转换后的图像显示为响应 [英] How to convert web stream getting from httpwebresponse to bitmap and how to show converted image as response

查看:262
本文介绍了如何将从httpwebresponse获取的Web流转换为位图,以及如何将转换后的图像显示为响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从httpwebresponse获取流,这是由另一个Web服务器发送的,这里我想将此Web流转换为Bitmap,然后这个转换后的图像用于在c#中显示为响应。



我尝试过:



i am getting stream from httpwebresponse which is send by the another web server, here i want to convert this web stream to Bitmap and then this converted image is used to show as response in c#.

What I have tried:

HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse myResp = (HttpWebResponse)myWebRequest.GetResponse())
                    { 
                        if (myResp.ContentType.Contains("image/jpeg"))
                        {
                            Stream myStream = myResp.GetResponseStream();

推荐答案

我看到两个选项......



1.将流中的所有字节读入数组,而不是使用BinaryWriter保存它。

2.使用Image.FromStream在内存中创建映像,而不是使用Image保存。保存
I see two options...

1. Read all bytes from the stream into an array than save it using BinaryWriter
2. Use Image.FromStream to create the image in-memory, than save using Image.Save


HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);request.Method = "POST";
myWebRequest .ContentType = "application/x-www-form-urlencoded";
myWebRequest .ContentLength = post_buffer.Length;
Stream request_stream = myWebRequest.GetRequestStream();
request_stream.Write(postBuffer, 0, postBuffer.Length);
request_stream.Close();
postBuffer = null;

//send the request, read the response
HttpWebResponse response = (HttpWebResponse)myWebRequest.GetResponse();
Stream response_stream = response.GetResponseStream();
Bitmap bitmap = new Bitmap(response_stream);
if(bitmap!=null)
{
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader read_stream = new StreamReader(response_stream, encode);
}





我还没有编译这个解决方案。请不要直接复制粘贴,你必须得到响应流然后可以来了解你是将图像作为回应还是其他东西。



I have not compiled this solution.Please do not copy paste directly and you have to get response stream then can come to know whether you are getting image as response or something else.


这篇关于如何将从httpwebresponse获取的Web流转换为位图,以及如何将转换后的图像显示为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆