Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
Posted
Updated 16-Jan-18 2:04am
v2

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
 
Share this answer
 
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.
 
Share this answer
 
Comments
rocker_003 16-Jan-18 8:10am    
is Response.Write(bitmap); used to show bitmap as response?
[no name] 16-Jan-18 8:17am    
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.bitmap);
stream.Position = 0;
byte[] data = new byte[stream.Length];
stream.Read(data, 0, stream.Length);
And then use the Response.BinaryWrite method to write it as binary
Response.BinaryWrite(data);
rocker_003 17-Jan-18 3:04am    
answer me for above comment...
rocker_003 17-Jan-18 3:03am    
bro.. how to convert bitmap to stream...?
[no name] 17-Jan-18 3:05am    
Please check the below line in above comment.
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.bitmap);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900