Sending Images Over a Network Stream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ollie Shotton
    New Member
    • Jan 2011
    • 12

    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:

    Code:
    private byte[] readItem(string file)
    {
    	byte[] output = null;
              
            try
            {
    		MemoryStream ms = new MemoryStream();
                    System.Drawing.Image i = System.Drawing.Image.FromFile(file);                    
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    
                    i.Dispose();
                    ms.Flush();
                    ms.Close();
                        
                    output = ms.ToArray();
    	}
            catch (Exception exp)
            {
                  //handles the error
      	      errorMessage(exp);
            }
    
    	return output;
    }
    OR

    Code:
    private string readItem(string file)
    {
        string output = null;
        byte[] temp = null;
    
            try
            {
                MemoryStream ms = new MemoryStream();
                System.Drawing.Image i = System.Drawing.Image.FromFile(file);
                i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
    
                i.Dispose();
                ms.Flush();
                ms.Close();
    
                temp = ms.ToArray();
                for(int a=0; a<temp.Length; a++)
                {
                    output += temp[a];
                }
            }
            catch (Exception exp)
            {
                //method that deals with the exp
                errorMessage(exp);
            }
        }
    
        return output;
    }
    if it helps am sending them like this:

    Code:
    	mainStream = new NetworkStream(connection);
    	inStream = new BinaryReader(mainStream);
    	outStream = new BinaryWriter(mainStream);
    
    	byte[] item = null;
    	//or
    	string item = null;
    	//depending on which method is called
    
    	/* here i make the http header...*/ 
    	
    	outStream.Write(head);
            outStream.Write(item);
    Thanks in advance =)
    Ollie
Working...