#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    11
    Rep Power
    0

    Serving images using HTTPListener


    I am using the HTTPListener object to serve requests for html documents. Everything seems straight forward. However I would also like to be able to dynamically create images based on requests.

    How do I go about sending images (such as a png) from a picturebox through the HTTPListener. I already guessed I'll need to change MIME type, but how do I get the image into the output stream?

    Thanks in advance!

    P.S. I am using C#
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    11
    Rep Power
    0

    Don't worry. I solved it myself


    After much playing around, I got this. It may be useful for anyone else who is interested:

    Code:
            protected virtual void ProcessRequest(HttpListenerContext Context)
            {
    
                HttpListenerRequest Request = Context.Request;
                HttpListenerResponse Response = Context.Response;
    
    // This bit creates the image and is obviously unique to my app
                int xx = Convert.ToInt32(Request.QueryString["x"]);
                int yy = Convert.ToInt32(Request.QueryString["y"]);
    
                DrawMap(buff,xx,yy,594,339,gBumps);
                
    //Save out image as file
                buff.Save("c:\\temp.png", System.Drawing.Imaging.ImageFormat.Png);
    
    //Open image as byte stream to send to requestor
                FileInfo fInfo = new FileInfo("c:\\temp.png");
                long numBytes = fInfo.Length;
    
                FileStream fStream = new FileStream("c:\\temp.png", FileMode.Open, FileAccess.Read);
    
                BinaryReader br = new BinaryReader(fStream);
    
                byte[] bOutput = br.ReadBytes((int)numBytes);
    
                br.Close();
    
                fStream.Close();
    
                Response.ContentType = "image/png";
                Response.ContentLength64 = bOutput.Length;
    
                Stream OutputStream = Response.OutputStream;
                OutputStream.Write(bOutput, 0, bOutput.Length);
                OutputStream.Close();
    
            }

IMN logo majestic logo threadwatch logo seochat tools logo