c# - Quickly sending base64 strings over web sockets? -
i'm attempting create video stream capturing , converting jpeg images base64 string, , sending using websockets (nodejs , socket.io) drawing on html5 canvas. code in c# capturing screen , sending using socketio4net library:
client directsocket = new client("http://iphere"); directsocket.connect(); private void timer1_tick(object sender, eventargs e) { rectangle bounds = screen.getbounds(point.empty); bitmap bitmap = new bitmap(bounds.width, bounds.height); using (graphics g = graphics.fromimage(bitmap)) { g.copyfromscreen(point.empty, point.empty, bounds.size); } picturebox1.image = bitmap; // convert picturebox byte[] array system.io.memorystream stream = new system.io.memorystream(); picturebox1.image.save(stream, system.drawing.imaging.imageformat.jpeg); byte[] imagebytes = stream.toarray(); // convert byte[] base64 string string base64string = convert.tobase64string(imagebytes); // send using socketio4net directsocket.emit("send", new { message = base64string }); } that works fine , base64string sent node server emits it. client-side javascript code:
var socket = io.connect('iphere'); socket.on('receive', function (data) { var canvas = document.getelementbyid('canvas'); var context = canvas.getcontext('2d'); var img = new image(); img.onload = function () { context.drawimage(img, 0, 0, canvas.width, canvas.height); }; img.src = "data:image/jpeg;base64," + data.message.message; }); my problem base64string large , isn't emitted fast enough client receive , display image on canvas. i'm not sure if it's node server (it's cheap vps) or if string large. possible compress base64string , send decompressing when it's received? or there way can it? help.
you try saving image jpg lower quality. can use save override accepts encoder , encoder parameters.
Comments
Post a Comment