I got help in a previous question on how to send an image. The thing done was to first send the lenght of the image(size), and then the actual image, then it would know when it was done.
IT looks like this:
BinaryWriter writer = new BinaryWriter(netStream);
while (someCondition) {
Image img = SomeImage();
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
writer.Write(buffer.Length);
writer.Write(buffer);
This code is from: Angelo Geels , who helped me in my previous question.
Now, i tried to optimize this in a way. And well, it works. But ONLY when the file is bmp (uncompressed), and i don´t know why.
using (MemoryStream ms = PrintWindow(process))
{
writer.Write((int)ms.Length);
writer.Write(ms.GetBuffer());
}
So PrintWindow save an image to a memorystream and returns it. so ms = memorystream with my image in it.
So for me this should work perfectly, cause form what i can se i do the same thing.
i send the size of the file (length of the memorystream). Then i send the byte[] data in the memorystream.
So, it´s the same thing.
But, it only works with bmp.
The only thing i can think of is that when i save in a compressed format, the bmp is first written and then encoded, which messes up the getbuffer() or something.
But i still think it should work.
You write too many bytes, use the Write() overload that lets you specify how much to write:
using (MemoryStream ms = PrintWindow(process)) {
writer.Write((int)ms.Length);
writer.Write(ms.GetBuffer(), 0, (int)ms.Length);
}