<tfoot id='Vy1uU'></tfoot>

    <i id='Vy1uU'><tr id='Vy1uU'><dt id='Vy1uU'><q id='Vy1uU'><span id='Vy1uU'><b id='Vy1uU'><form id='Vy1uU'><ins id='Vy1uU'></ins><ul id='Vy1uU'></ul><sub id='Vy1uU'></sub></form><legend id='Vy1uU'></legend><bdo id='Vy1uU'><pre id='Vy1uU'><center id='Vy1uU'></center></pre></bdo></b><th id='Vy1uU'></th></span></q></dt></tr></i><div id='Vy1uU'><tfoot id='Vy1uU'></tfoot><dl id='Vy1uU'><fieldset id='Vy1uU'></fieldset></dl></div>
  1. <small id='Vy1uU'></small><noframes id='Vy1uU'>

      • <bdo id='Vy1uU'></bdo><ul id='Vy1uU'></ul>
      <legend id='Vy1uU'><style id='Vy1uU'><dir id='Vy1uU'><q id='Vy1uU'></q></dir></style></legend>

      将字节转换为图像以在 HTML5 画布上绘图

      时间:2023-06-20

              <bdo id='JZuFW'></bdo><ul id='JZuFW'></ul>

              <tfoot id='JZuFW'></tfoot>
                <legend id='JZuFW'><style id='JZuFW'><dir id='JZuFW'><q id='JZuFW'></q></dir></style></legend>

              • <small id='JZuFW'></small><noframes id='JZuFW'>

                <i id='JZuFW'><tr id='JZuFW'><dt id='JZuFW'><q id='JZuFW'><span id='JZuFW'><b id='JZuFW'><form id='JZuFW'><ins id='JZuFW'></ins><ul id='JZuFW'></ul><sub id='JZuFW'></sub></form><legend id='JZuFW'></legend><bdo id='JZuFW'><pre id='JZuFW'><center id='JZuFW'></center></pre></bdo></b><th id='JZuFW'></th></span></q></dt></tr></i><div id='JZuFW'><tfoot id='JZuFW'></tfoot><dl id='JZuFW'><fieldset id='JZuFW'></fieldset></dl></div>
                  <tbody id='JZuFW'></tbody>
                本文介绍了将字节转换为图像以在 HTML5 画布上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有人知道我如何将通过 websocket(从 C# 应用程序)发送的字节转换为图像吗?然后我想在画布上绘制图像.我可以看到两种方法:

                Anyone know how I would convert bytes which are sent via a websocket (from a C# app) to an image? I then want to draw the image on a canvas. I can see two ways of doing this:

                • 不知何故以字节形式在画布上绘制图像而不转换它.
                • 然后在javascript中以某种方式将字节转换为base64字符串画.

                这是我接收绘图字节的函数:

                Here's my function which receives the bytes for drawing:

                function draw(imgData) {
                
                    var img=new Image();
                    img.onload = function() {
                        cxt.drawImage(img, 0, 0, canvas.width, canvas.height);
                    };
                
                // What I was using before...
                img.src = "data:image/jpeg;base64,"+imgData;
                
                }
                

                我之前收到的图像已经转换为 base64 字符串,但在得知发送字节的大小更小(小 30%?)之后,我更愿意让它工作.我还应该提到图像是 jpeg.

                I was receiving the image already converted as a base64 string before, but after learning that sending the bytes is smaller in size (30% smaller?) I would prefer to get this working. I should also mention that the image is a jpeg.

                有人知道我会怎么做吗?谢谢您的帮助.:)

                Anyone know how I would do it? Thanks for the help. :)

                推荐答案

                我最后用了这个:

                function draw(imgData, frameCount) {
                    var r = new FileReader();
                    r.readAsBinaryString(imgData);
                    r.onload = function(){ 
                        var img=new Image();
                        img.onload = function() {
                            cxt.drawImage(img, 0, 0, canvas.width, canvas.height);
                        }
                        img.src = "data:image/jpeg;base64,"+window.btoa(r.result);
                    };
                }
                

                在使用 btoa() 之前,我需要将字节读入字符串.

                I needed to read the bytes into a string before using btoa().

                这篇关于将字节转换为图像以在 HTML5 画布上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:three.js 2D 文本精灵标签 下一篇:HTML5 将图像从工具栏拖放到画布

                相关文章

              • <i id='s6rJi'><tr id='s6rJi'><dt id='s6rJi'><q id='s6rJi'><span id='s6rJi'><b id='s6rJi'><form id='s6rJi'><ins id='s6rJi'></ins><ul id='s6rJi'></ul><sub id='s6rJi'></sub></form><legend id='s6rJi'></legend><bdo id='s6rJi'><pre id='s6rJi'><center id='s6rJi'></center></pre></bdo></b><th id='s6rJi'></th></span></q></dt></tr></i><div id='s6rJi'><tfoot id='s6rJi'></tfoot><dl id='s6rJi'><fieldset id='s6rJi'></fieldset></dl></div>

                <legend id='s6rJi'><style id='s6rJi'><dir id='s6rJi'><q id='s6rJi'></q></dir></style></legend>
              • <small id='s6rJi'></small><noframes id='s6rJi'>

                <tfoot id='s6rJi'></tfoot>
                    <bdo id='s6rJi'></bdo><ul id='s6rJi'></ul>