title: Got Reversed Image from Byte Array when converting to Base64
body: <p>I need to get the raw bitmap data only (no header, or other information). I used the following code to get the bitmap data:</p>
  
  <pre><code>using (Bitmap bitmap = svgDocument.Draw())
  {
    Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
    BitmapData bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);
    var length = Math.Abs(bitmapData.Stride) * bitmapData.Height;
  
    byte[] bytes = new byte[length];
    Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
    bitmap.UnlockBits(bitmapData);
  
    MemoryStream memoryStream = new MemoryStream();
    string filename = DateTime.Now.Ticks.ToString() + ".bmp"; // this works fine
    bitmap.Save(filename, ImageFormat.Bmp);
  
    string base64 = Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); // the base64 is reversed. 
  }
  </code></pre>
  
  <p>When I save the bitmap, everything looks fine. The image is not reversed. However when I use the bytes only to convert the data to Base64, then the image is reversed.</p>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>I think this has nothing to do with the Base64 conversion. It seems that the bytes are in reversed order. </p>
  
  <p>When I save the image using the code, the image looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/0EQmQ.png" alt="enter image description here"></p>
  
  <p>When I use the bytes, then I see this:</p>
  
  <p><img src="https://i.stack.imgur.com/wR8Gt.png" alt="enter image description here"></p>
  
  <p><strong>Solution:</strong></p>
  
  <p>I found the solution. Instead of creating a new bitmap, I just skipped the first 54 bytes of header information and then stored the byte array. </p>
  
  <pre><code>MemoryStream memoryStream = new MemoryStream();
  bitmap.Save(memoryStream, ImageFormat.Bmp);
  
  // Skip header
  IEnumerable&lt;byte&gt; bytes = memoryStream.ToArray().Skip(54);
  </code></pre>