I programmed a EMX based webserver which can also show pictures which ultimately should come from a webcam. To test the code i used a bmp file as resource and also used the same file renamed as bin file. If i send the bin file straight into the socket i get the correct result. However if i retreive the bitmap as bitmap resource and use the Util.BitmapToBMPFile as described in the API example i get a scrambled image with firefox and the internet explorer terminates the connection right after the first 1024 bites.
Code:
if (requestLine[1] == “/image.bmp”)
{
//byte[] pictureBuffer = Resources.GetBytes(Resources.BinaryResources.imageBin);
Bitmap image = Resources.GetBitmap(Resources.BitmapResources.image);
byte[] pictureBuffer = new byte[image.Width * image.Height * 3 + 54];
Util.BitmapToBMPFile(image.GetBitmap(), image.Width, image.Height, pictureBuffer);
string header = "";
header += "HTTP/1.1 200 OK" + "\r\n";
header += "Content-Type: image/bmp" + "\r\n";
header += "Content-Length: " + pictureBuffer.Length + "\r\n";
header += "\r\n";
byte[] sendBuffer = new byte[1024];
int sendOffset = 0;
while (sendOffset < (Encoding.UTF8.GetBytes(header).Length + pictureBuffer.Length) )
{
int length = System.Math.Min(sendBuffer.Length, (Encoding.UTF8.GetBytes(header).Length + pictureBuffer.Length) - sendOffset);
sendBuffer = null;
sendBuffer = new byte[length];
byte[] outputArray = new byte[Encoding.UTF8.GetBytes(header).Length + pictureBuffer.Length];
Encoding.UTF8.GetBytes(header).CopyTo(outputArray, 0);
pictureBuffer.CopyTo(outputArray, Encoding.UTF8.GetBytes(header).Length);
Array.Copy(outputArray, sendOffset, sendBuffer, 0, length);
sendOffset += length;
clientSock.Send(sendBuffer);
}
clientSock.Close();
}