title: Convert from Yuv 420 to RGB and then to Bitmap
body: <p>I'm having an issue converting image from <code>byte YUV420p[]</code> to <code>byte RGB[]</code> and then to a <code>Bitmap</code>.</p>
  <p>This is method to convert from YUV to RGB that I'm using:</p>
  <pre><code>    double[,] YUV2RGB_CONVERT_MATRIX = new double[3, 3] 
      {
          { 1, 0, 1.4022 }, 
          { 1, -0.3456, -0.7145 }, 
          { 1, 1.771, 0 } 
      };
      
      unsafe Bitmap ConvertYUV2RGB(byte[] YUVFrame, int width, int height)
      {
          int uIndex = width * height;
          int vIndex = uIndex + ((width * height) &gt;&gt; 2);
          int gIndex = width * height;
          int bIndex = gIndex * 2;
          byte[] rgbFrame = new byte[uIndex * 3];
  
          //图片为pic1,RGB颜色的二进制数据转换得的int r,g,b;
          Bitmap bitmap = new Bitmap(width, height);
  
          for (int y = 0; y &lt; height; y++)
          {
              for (int x = 0; x &lt; width; x++)
              {
                  // R分量
                  int temp = (int)(YUVFrame[y * width + x] + (YUVFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
                  rgbFrame[y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
                  // G分量
                  temp = (int)(YUVFrame[y * width + x] + (YUVFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (YUVFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
                  rgbFrame[gIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
                  // B分量
                  temp = (int)(YUVFrame[y * width + x] + (YUVFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
                  rgbFrame[bIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
  
                  System.Drawing.Color c = System.Drawing.Color.FromArgb(RGBFrame[y * width + x], RGBFrame[gIndex + y * width + x], RGBFrame[bIndex + y * width + x]);
                  bitmap.SetPixel(x, y, c);
  
              }
          }
  
          return bitmap;
      }
  </code></pre>
  <p>That function works 100% but it is very slow for obvious reasons:</p>
  <pre><code>                System.Drawing.Color c = System.Drawing.Color.FromArgb(RGBFrame[y * width + x], RGBFrame[gIndex + y * width + x], RGBFrame[bIndex + y * width + x]);
                  bitmap.SetPixel(x, y, c);
  </code></pre>
  <p>Here's the generated image:</p>
  <p><a href="https://i.stack.imgur.com/fEoFG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fEoFG.png" alt="enter image description here" /></a></p>
  <p>So, to avoid calling <code>bitmap.SetPixel(x, y, c)</code> inside the loop I changed the code to:</p>
  <pre><code>    unsafe Bitmap ConvertYUV2RGB(byte[] YUVFrame, int width, int height)
      {
          int uIndex = width * height;
          int vIndex = uIndex + ((width * height) &gt;&gt; 2);
          int gIndex = width * height;
          int bIndex = gIndex * 2;
          byte[] RGBFrame = new byte[uIndex * 3];
  
          //图片为pic1,RGB颜色的二进制数据转换得的int r,g,b;
          //Bitmap bitmap = new Bitmap(width, height);
  
          for (int y = 0; y &lt; height; y++)
          {
              for (int x = 0; x &lt; width; x++)
              {
                  // R分量
                  int temp = (int)(YUVFrame[y * width + x] + (YUVFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[0, 2]);
                  RGBFrame[y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
                  // G分量
                  temp = (int)(YUVFrame[y * width + x] + (YUVFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 1] + (YUVFrame[vIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[1, 2]);
                  RGBFrame[gIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
                  // B分量
                  temp = (int)(YUVFrame[y * width + x] + (YUVFrame[uIndex + (y / 2) * (width / 2) + x / 2] - 128) * YUV2RGB_CONVERT_MATRIX[2, 1]);
                  RGBFrame[bIndex + y * width + x] = (byte)(temp &lt; 0 ? 0 : (temp &gt; 255 ? 255 : temp));
  
                  // Commented to avoid calling functions from inside the for loop
                  // System.Drawing.Color c = System.Drawing.Color.FromArgb(RGBFrame[y * width + x], RGBFrame[gIndex + y * width + x], RGBFrame[bIndex + y * width + x]);
                  // bitmap.SetPixel(x, y, c);
              }
          }
  
          return CreateBitmap(RGBFrame, width, height);
      }
  
      private Bitmap CreateBitmap(byte[] RGBFrame, int width, int height)
      {
          PixelFormat pxFormat = PixelFormat.Format24bppRgb;
          Bitmap bmp = new Bitmap(width, height, pxFormat);
          BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pxFormat);
  
          IntPtr pNative = bmpData.Scan0;
          Marshal.Copy(RGBFrame, 0, pNative, RGBFrame.Length);
          bmp.UnlockBits(bmpData);
          return bmp;
      }
  </code></pre>
  <p>But I'm unable to create the image correctly. This is the result:</p>
  <p><a href="https://i.stack.imgur.com/R8VRO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R8VRO.png" alt="enter image description here" /></a></p>
  <p>What is going on here?</p>