1

im just using a faster method to convert image to byte array it's not new i found it in some places over the internet.

this is the send code

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


        datasize = BitConverter.GetBytes(size);
        sent = s.Send(datasize);


            sent = s.Send(data, total, dataleft, SocketFlags.None);
            total += sent;
            dataleft -= sent;
          //  MessageBox.Show("D");

        return total;
    }

and i call it :

    SendVarData(handler, bmpBytes);

where handler is a socket and bmpbytes is the image converted to byte array. do u have any better solution? @xanatos

4
  • 1
    Because you are probably loading a jpg or a gif image that is compressed... Try saving it to a bmp and see the size. Commented Jun 30, 2015 at 12:52
  • @xanatos thank you very much now i understand haha. but is there any way to compress it now using the first unsafe method? Commented Jun 30, 2015 at 12:54
  • The code is correct. The only thing you can try, as I wrote, is to create a NetworkStream just to send the bitmap and see if it is faster or not. Commented Jun 30, 2015 at 13:59
  • @xanatos allright i'll try it. a huge thanks. Commented Jun 30, 2015 at 14:01

1 Answer 1

5

It's not weird at all. In your first sample, you're copying the image as it is stored in memory, i.e. uncompressed.

In your second sample you're getting the data as it would have been stored on disk, i.e. compressed as GIF in this case.

Sign up to request clarification or add additional context in comments.

11 Comments

oh i understand now. is there any one to compress it with the first unsafe method?
You're doing two completely different things. If you want it compressed, stick to the second method.
@itapi The second method is slow because it is compressing the image.
The reason it's slower is because it's compressing the image. Maybe there's some heavily optimized GIF compression libraries out there somewhere, but I'd think the code in GDI would be pretty good.
@itapi It is probably possible to build a faster MemoryStream, but I don't think you would gain very much. To see how much it can be speedup, try constructing the MemoryStream with new MemoryStream(1000000). If it is much faster (I don't think), I'll write some line of code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.