title: Opencv - create png image
body: <p>As part of my project I wanted to send stream of images using websockets from embedded machine to client application and display them in <code>img</code> tag to achieve streaming.</p>
  
  <p>Firstly I tried to send raw RGB data (752*480*3 - something about 1MB) but in the end I got some problems with encoding image to png in javascript based on my RGB image so I wanted to try to encode my data to PNG firstly and then sent it using websockets.</p>
  
  <p>The thing is, I am having some problems with encoding my data to PNG using <code>OpenCV</code> library that is already used in the project.</p>
  
  <p>Firstly, some code:</p>
  
  <pre><code>websocketBrokerStructure.matrix = cvEncodeImage(0, websocketBrokerStructure.bgrImageToSend, 0);
  websocketBrokerStructure.imageDataLeft = websocketBrokerStructure.matrix-&gt;rows * websocketBrokerStructure.matrix-&gt;cols *  websocketBrokerStructure.matrix-&gt;step;
  websocketBrokerStructure.imageDataSent = 0;
  </code></pre>
  
  <p>but I am getting strange error during execution of the second line:</p>
  
  <pre><code>terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid
  </code></pre>
  
  <p>and I am a bit confused why I am getting this error from my code.</p>
  
  <p>Also I am wondering if I understand it right: after invoking cvEncodeImage (where <code>bgrImage</code> is <code>IplImage*</code> with 3 channels - BGR) I just need to iterate through <code>data</code> member of my <code>CvMat</code>to get all of the png encoded data?</p>