473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Sending Images Over a Network Stream

Hi i am making a simple HTTP Server using TCP/IP sockets for a project.

i have a method to get an image and send it over to the browser but i am having problems doing so, i have tried doing it different ways but nothing seems to be working.

the browser asks for the image, the server sends the head along with the image but the browser never shows the image. the server works fine for all the html/css files i have send to the browser.

Please help me!!!

Here is two visions of the method, both which did work:

Expand|Select|Wrap|Line Numbers
  1. private byte[] readItem(string file)
  2. {
  3.     byte[] output = null;
  4.  
  5.         try
  6.         {
  7.         MemoryStream ms = new MemoryStream();
  8.                 System.Drawing.Image i = System.Drawing.Image.FromFile(file);                    
  9.                 i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  10.  
  11.                 i.Dispose();
  12.                 ms.Flush();
  13.                 ms.Close();
  14.  
  15.                 output = ms.ToArray();
  16.     }
  17.         catch (Exception exp)
  18.         {
  19.               //handles the error
  20.             errorMessage(exp);
  21.         }
  22.  
  23.     return output;
  24. }
OR

Expand|Select|Wrap|Line Numbers
  1. private string readItem(string file)
  2. {
  3.     string output = null;
  4.     byte[] temp = null;
  5.  
  6.         try
  7.         {
  8.             MemoryStream ms = new MemoryStream();
  9.             System.Drawing.Image i = System.Drawing.Image.FromFile(file);
  10.             i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  11.  
  12.             i.Dispose();
  13.             ms.Flush();
  14.             ms.Close();
  15.  
  16.             temp = ms.ToArray();
  17.             for(int a=0; a<temp.Length; a++)
  18.             {
  19.                 output += temp[a];
  20.             }
  21.         }
  22.         catch (Exception exp)
  23.         {
  24.             //method that deals with the exp
  25.             errorMessage(exp);
  26.         }
  27.     }
  28.  
  29.     return output;
  30. }
if it helps am sending them like this:

Expand|Select|Wrap|Line Numbers
  1.     mainStream = new NetworkStream(connection);
  2.     inStream = new BinaryReader(mainStream);
  3.     outStream = new BinaryWriter(mainStream);
  4.  
  5.     byte[] item = null;
  6.     //or
  7.     string item = null;
  8.     //depending on which method is called
  9.  
  10.     /* here i make the http header...*/ 
  11.  
  12.     outStream.Write(head);
  13.         outStream.Write(item);
Thanks in advance =)
Ollie
Apr 16 '11 #1
0 1676

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John | last post by:
I have a Socket open to a target system. I get the network stream from the open socket and then create a stream reader and stream writer on this stream. The stream reader and writers are operating...
2
by: Kueishiong Tu | last post by:
I have a network stream which I got from HttpWebResponse and does not support seeking, How do I find the length of the network stream? HttpWebResponse* response = dynamic_cast<HttpWebResponse*>...
6
by: Yechezkal Gutfreund | last post by:
I have been using the following code (successfully) to read Xml formated text packets from a TCP stream. The output from the server stream consists of a sequence of well formed Xml documents...
5
by: cs | last post by:
I need to get the position and also seek in a network stream, is there an easy way to do that? (I am converting some java code so thats the easy path).
8
by: Scott | last post by:
Hi guys, If I try to call read(), readline(), readtoend() and there is nothing to read (from a never ending loop for example) the program seems to continue but it exits the loop for no apparent...
7
by: simonrigby_uk | last post by:
Hi all, Sorry if this is the incorrect group but I couldn't see anything directly relevant. Can someone confirm for me what happens when two network streams are sent to an application at the...
11
by: Detlef Hüttenbach | last post by:
Whereas loading tiffs and pngs from file into an Image WebControl work, the images aren't shown when loaded from streams: In a WebForm, the image's property "ImageUrl" is set to a handler, and...
3
by: Alex Clark | last post by:
Hi All, I'm having some problems reading a network stream. I'm writing a lightweight POP client to handle a very specific task, but I keep unexpectedly reaching the end of the datastream when...
3
by: Al G | last post by:
Does anyone know what a -1 means as a Network stream read timeout? Console.WriteLine("Timeout= {0}", stream.ReadTimeout) returns a -1 Thanks
0
by: cva cangaran | last post by:
server side coding TcpClient dupClient = listener.AcceptTcpClient(); NetworkStream ns = dupClient.GetStream(); StreamReader sr = new StreamReader(ns); StreamWriter sw = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.