views:

464

answers:

2

I need fastest way to transfer bitmap object over the lan in C#.

Abdul Khaliq

+2  A: 

The word "best" is, at best, a subjective term :-)

If you're talking about speed, then it depends on how big the bitmap is. With a LAN running at 100Mbps, you can expect something roughly around 1 second for each 10MB of file. For a small file, just transfer it as-is. At some file size, it will become worth zipping the file up, transmitting it and unzipping it at the other end, simply because CPU grunt is faster than network grunt.

But I think you'd be talking about pretty big files to make that extra coding worthwhile.

Update:

Since you talk about a screen capture frame, let's say we're at 1280x1024, 32bpp. A full screen will then take up 5M which should be transferable on a 100Mbps LAN in under a second (other network traffic permitting). It's not worth it, in my opinion, to try and speed that up any more since the overhead of compression would outweigh the savings in time.

If you will be doing video transfer, that's another matter. Then you wouldn't send a brand new image for every frame - you'd work out the deltas and transfer only that information, relying on the fact that the screen generally only changes a little each frame.

It depends on what you're going to use the images for. A one-shot screen capture, just send the binary information. Anything else, well, you'll need to give us more detail.

For video transfer, we've actually done some work on different compression methods.

The simplest you may want to look at is to divide the screen into a (e.g.) 16x16 matrix and only transmit the changed elements.

So, for example, each frame will consist of a 256-bit bitmask indicating which elements have changed. Then, that bitmask is followed by the elements themselves.

That algorithm means the smallest frame delta is 32 bytes (if the screen hasn't changed). The largest is only 32 bytes larger than a full screen dump.

One of the other methods we used was to simply store the topleft-most and bottomright-most pixel positions that had changed and transfer those two values along with the entire rectangle bounded by them.

No doubt there are other methods you could use, even choosing the method dynamically on a frame-by-frame basis to ensure minimum delta size.

paxdiablo
the bitmap object is almost 3000 KB in size in and i need to send it very frequently over the lan like screen capturing. think about send it 11 bitmap objects in one second. so what is the fastest possible way?
Abdul Khaliq
See the update, you should NOT be sending the full screen dump in every frame. Use algorithms to detect what part of the screen has changed and send the delta only.
paxdiablo
well we are already using mirror driver to calculate the deltas on the screen and taking unions and intersections to further reduce the number of bitmaps objects to be sent over the lan. The problem is no calculating the deltas but how to transfer them efficiently over the lan.
Abdul Khaliq
Then you may need to clarify the question further. Based on the information you've provided, you won't find a faster way to transfer a delta than just keeping a socket open and sending the raw binary data of the deltas down as you figure them out. The deltas will be small enough that you won't gain anything by compression. You're more likely to gain speed from optimizing the delta calculations, if anything.
paxdiablo
well i have already develop the optimized algo for calculating the minimum bitmaps required to be sent at one instance
Abdul Khaliq
That's fine, Abdul. If you're confident you have the best algorithms for creating the deltas, my comment stands: compression of a delta using general methods won't give you a real advantage - just blit the bits over as they are. Special compression methods may be worth looking at (e.g., using a palette of colors rather than a fixed 32bpp) but you'll always have to balance the extra time spent processing against the saved time spent transmitting).
paxdiablo
can u please further elaborate on the special compression algorithms that i can use with out waisting much cup cycles
Abdul Khaliq
I've given you a couple (the 16x16 matrix and the topleft/bottomright pixel methods) - for our application, they relied on being able to intercept pixel changes in the video driver for performance, diffing the old image and current image may not be fast enough (you'll need to try to see if they are).
paxdiablo
A: 

here's a bug in the .NET V2.0 SP1 and .NET 3.5 version of CopyFromScreen(). It leaks a handle, after a while you'll run out available handles and get weirdo error messages like this. You can't use it in its present condition, check this thread for another way to do it by P/Invoking Windows API functions.

also there is a nice solution found on this link.

Abdul Khaliq