C# MemoryStream to Image problem

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
 
A

Alberto Poblacion

Ben said:
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.
[...]
Does anybody have any ideas on how to solve this?

I don't know what's wrong with your code, but I have a suggestion: Why
don't you use an HttpWebRequest, and then assign the ResponseStream to the
Image.FromStream? It's much shorter and easier to understand. I have used t
before and works very well.

HttpWebRequest req= (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp= (HttpWebResponse)req.GetResponse();
Stream stm = resp.GetResponseStream();
System.Drawing.Image img = new Bitmap(stm);
stm.Close();
 
P

Peter Duniho

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.

I see two problems, both fatal. For one, you don't account for the length
of the string you're searching for, so the "\r\n\r\n" substring you've
found is still included in the MemoryString you're trying to use to
initialize the Image. Of course, that's not a valid set of bytes for the
beginning of an Image, so it fails.

The other problem is that you are not doing anything to ensure that you've
received all of the data for the image. For any image of any significant
size, you aren't going to get all of the data in a single call to
Socket.Receive(). You need to keep calling it and adding the data to the
MemoryStream until you've reached the end of the data (the header should
tell you how many bytes to expect, if I recall correctly), and only then
try to create an Image from it.

Either of those problems alone would prevent you from correctly creating
an Image. You need to fix both in order for your code to be reliable.

Pete
 
B

Ben

Thank you both :)

I'm new to the sockets thing, and don't quite understand it yet.
I will try Alberto's suggestion.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top