Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# send image over HTTP

I have a small HTTP-Server here written in C# and until now I only needed to send raw text back to the sender. But now I have to send a JPG-Image and I dont unterstand how.

this is what I have now:

// Read the HTTP Request
Byte[] bReceive = new Byte[MAXBUFFERSIZE];
int i = socket.Receive(bReceive, bReceive.Length, 0);

//Convert Byte to String
string sBuffer = Encoding.ASCII.GetString(bReceive);

// Look for HTTP request
iStartPos = sBuffer.IndexOf("HTTP", 1);

// Extract the Command without GET_/ at the beginning and _HTTP at the end
sRequest = sBuffer.Substring(5, iStartPos - 1 - 5);
String answer = handleRequest(sRequest);


// Send the response
socket.Send(Encoding.UTF8.GetBytes(answer));

I think I have to do some kind of filestream instead of a string but I really have no glue..

like image 600
martinyyyy Avatar asked Nov 11 '10 13:11

martinyyyy


2 Answers

Do you want to send from file or Bitmap object?

MemoryStream myMemoryStream = new MemoryStream();
myImage.Save(myMemoryStream);
myMemoryStream.Position = 0;

EDIT

// Send the response
SendVarData(socket,memoryStream.ToArray());

for sending MemoryStream by socket you can use this method given here

 private static int SendVarData(Socket s, byte[] data)
 {
            int total = 0;
            int size = data.Length;
            int dataleft = size;
            int sent;

            byte[] datasize = new byte[4];
            datasize = BitConverter.GetBytes(size);
            sent = s.Send(datasize);

            while (total < size)
            {
                sent = s.Send(data, total, dataleft, SocketFlags.None);
                total += sent;
                dataleft -= sent;
            }
            return total;
 }
like image 141
Arsen Mkrtchyan Avatar answered Nov 11 '22 23:11

Arsen Mkrtchyan


Why did you create a httpserver by yourself? Why not use a open source one? For instance mine: http://webserver.codeplex.com

public class Example    
{
    private HttpListener _listener;

    public void StartTutorial()
    {
        _listener = HttpListener.Create(IPAddress.Any, 8081);
        _listener.RequestReceived += OnRequest;
        _listener.Start(5);
    }

    private void OnRequest(object source, RequestEventArgs args)
    {
        IHttpClientContext context = (IHttpClientContext)source;
        IHttpRequest request = args.Request;

        IHttpResponse response = request.CreateResponse(context);
        response.Body = new FileStream("Path\\to\\file.jpg");
        response.ContentType = "image\jpeg";
        response.Send();
    }

}

Edit

If you really want do do it by yourself:

string responseHeaders = "HTTP/1.1 200 The file is coming right up!\r\n" +
"Server: MyOwnServer\r\n" +
"Content-Length: " + new FileInfo("C:\\image.jpg").Length + "\r\n" +
"Content-Type: image/jpeg\r\n" + 
"Content-Disposition: inline;filename=\"image.jpg;\"\r\n" +
"\r\n";

//headers should ALWAYS be ascii. Never UTF8
var headers = Encoding.ASCII.GetBytes(responseHeaders); 
socket.Send(headers, 0, headers.Length);
socket.SendFile("C:\\image.jpg");
like image 42
jgauffin Avatar answered Nov 12 '22 01:11

jgauffin