title: Object Size changed after converting an object to ByteArray
body: <p>I had a method to convert object to byte[] so as to populate PDF in browsers. I can see the inline PDF perfectly, however if I clicked "Save" button and open the downloaded file from my local it will give me the following message:</p>
  
  <blockquote>
    <p>"Adobe Reader could not open xx.pdf because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)"</p>
  </blockquote>
  
  <p>I found out that the data <strong>size difference</strong> during datatype conversion might cause the problem. The size of pdfDataObject pulled out from the database is {byte[1111161]}, which is the same as before it was stored into database; the converted pdfdata is {byte[1111189]}, which increases 28 bytes during the process of conversion. </p>
  
  <p>The datatype of pdf in database is varbinary(max)</p>
  
  <pre><code>public byte[] populated_PDF(string args)
  {
      DataTable dt = new DataTable();
  
      //sqlconnection to get data
  
      var pdfDataObject = dt.Rows[0]["PDF_DATA"];   
      // get the Varbinary datatype from database
      // The Data Size of pdfDataObject is {byte[1111161]}
  
      byte[] pdfdata = ObjectToByteArray(pdfDataObject);
      // The Data Size of pdfdata is       {byte[1111189]}
  
      return pdfdata;
  }
  </code></pre>
  
  <p>object to ByteArray method</p>
  
  <pre><code>private byte[] ObjectToByteArray(Object obj)
  {
      if (obj == null)
      {
          return null;
      }
      var bf = new BinaryFormatter();
      using (var ms = new MemoryStream())
      {
          bf.Serialize(ms, obj);
          return ms.ToArray();
      }
  }
  </code></pre>
  
  <p>The PDF can be displayed in browsers in both size of data. However the one with data size of {byte[1111189]} CANNOT be opened from local if "Save" is applied where the one with data size of {byte[1111161]} can.</p>
  
  <p>Seems like the method ObjectToByteArray added something which causes the failure of opening local downloaded PDF during conversion process.</p>
  
  <p>Don't know how to solve it. Any thoughts?</p>