title: Calculating the required buffer size for the WriteableBitmap.WritePixels method
body: <p>How do I calculate the required buffer size for the WriteableBitmap.WritePixels method?</p>
  
  <p>I am using the overload taking four parameters, the first is an Int32Rect, the next is a byte array containing the RGBA numbers for the colour, the third is the stride (which is the width of my writeable bitmap multiplied by the bits per pixel divided by 8), and the last is the buffer (referred to as the offset in Intellisense).</p>
  
  <p>I am getting the <strong>Buffer size is not sufficient</strong> runtime error in the below code:</p>
  
  <pre><code>byte[] colourData = { 0, 0, 0, 0 };
  
  var xCoordinate = 1;
  var yCoordinate = 1;
  
  var width = 2;
  var height = 2;
  
  var rect = new Int32Rect(xCoordinate, yCoordinate, width, height);
  
  var writeableBitmap = new WriteableBitmap(MyImage.Source as BitmapSource);
  
  var stride = width*writeableBitmap.Format.BitsPerPixel/8;
  
  writeableBitmap.WritePixels(rect, colourData, stride,0);
  </code></pre>
  
  <p>What is the formula I need to use to calculate the buffer value needed in the above code?</p>