B
Ben
Hello,
I'm trying to get an image from a webserver here in the network. I
managed to create a socket connection and filter out the response
headers. I load the body of the reply into a MemoryStream, but when
trying to use Image.FromStream(), I get an invalid argument error.
Here is my code:
public Image loadImage(DateTime time)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
sock.Connect("192.168.1.12", 80);
string year = curdate.Year.ToString();
year = year.Substring(year.Length - 2);
string month = curdate.Month.ToString().PadLeft(2, '0');
string day = curdate.Day.ToString().PadLeft(2, '0');
string header = "GET /garfield/garfield_coll/ga" + year +
month + day + ".gif HTTP/1.0\r\n";
header += "Host: 192.168.1.12\r\n\r\n";
textBox1.Text = "Sending request:\r\n\r\n" + header + "?";
sock.Send(Encoding.ASCII.GetBytes(header));
textBox1.Text += ("Request sent...\r\n");
byte[] bbuffer = new byte[sock.ReceiveBufferSize];
sock.Receive(bbuffer);
textBox1.Text += ("Reply received...\r\n");
MemoryStream mstream;
char[] reply = Encoding.ASCII.GetChars(bbuffer);
string replystr = new string(reply);
Regex rg = new Regex("\\r\\n\\r\\n");
MatchCollection mc = rg.Matches(replystr);
int i = mc[0].Index;
mstream = new MemoryStream();
mstream.Write(bbuffer, i, bbuffer.Length - i);
sock.Disconnect(false);
return Image.FromStream(mstream);
}
Does anybody have any ideas on how to solve this?
Thanks,
Ben
I'm trying to get an image from a webserver here in the network. I
managed to create a socket connection and filter out the response
headers. I load the body of the reply into a MemoryStream, but when
trying to use Image.FromStream(), I get an invalid argument error.
Here is my code:
public Image loadImage(DateTime time)
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
sock.Connect("192.168.1.12", 80);
string year = curdate.Year.ToString();
year = year.Substring(year.Length - 2);
string month = curdate.Month.ToString().PadLeft(2, '0');
string day = curdate.Day.ToString().PadLeft(2, '0');
string header = "GET /garfield/garfield_coll/ga" + year +
month + day + ".gif HTTP/1.0\r\n";
header += "Host: 192.168.1.12\r\n\r\n";
textBox1.Text = "Sending request:\r\n\r\n" + header + "?";
sock.Send(Encoding.ASCII.GetBytes(header));
textBox1.Text += ("Request sent...\r\n");
byte[] bbuffer = new byte[sock.ReceiveBufferSize];
sock.Receive(bbuffer);
textBox1.Text += ("Reply received...\r\n");
MemoryStream mstream;
char[] reply = Encoding.ASCII.GetChars(bbuffer);
string replystr = new string(reply);
Regex rg = new Regex("\\r\\n\\r\\n");
MatchCollection mc = rg.Matches(replystr);
int i = mc[0].Index;
mstream = new MemoryStream();
mstream.Write(bbuffer, i, bbuffer.Length - i);
sock.Disconnect(false);
return Image.FromStream(mstream);
}
Does anybody have any ideas on how to solve this?
Thanks,
Ben