title: Storing MemoryStream File to Azure Blob
body: <p>I have an image that gets dynamically generated via System.Drawing. I am then outputting the generated image to a <code>MemoryStream</code> for storage into my Azure blob.</p>
  <p>But I can't seem to get my file to store in the blob of my choice. There are no errors occurring and my image is successfully getting saved to <code>MemoryStream</code>. As expected, my blob is empty.</p>
  <p>I have ensured my blob container has public read/write access.</p>
  <h2>Code</h2>
  <pre><code>// Retrieve storage account from connection string.
  CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format(&quot;DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}&quot;, Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(&quot;CMSAzureAccountName&quot;).ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(&quot;CMSAzureSharedKey&quot;).ToString()));
  
  // Create the blob client.
  CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  
  // Retrieve reference to a previously created container.
  CloudBlobContainer container = blobClient.GetContainerReference(&quot;myimagecontainer&quot;);
  
  // Retrieve reference to a blob named &quot;myblob&quot;.
  CloudBlockBlob blockBlob = container.GetBlockBlobReference(&quot;test.jpg&quot;);
              
  //Output image
  ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
  EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
  Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
  
  System.IO.MemoryStream msImage = new System.IO.MemoryStream();
  GenerateImage.Render(imageOutput).Save(msImage, Info[1], Params); //GenerateImage.Render() method creates a custom image and returns a Bitmap
              
  // Create or overwrite the &quot;myblob&quot; blob with contents from a local file.
  using (var fileStream = msImage)
  {
      blockBlob.UploadFromStream(fileStream);
  }
  </code></pre>
  <p>Any help would be appreciated.</p>
  <h2>Update</h2>
  <p>I managed to find what the main cause of the error. I needed to change the following:</p>
  <pre><code>CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format(&quot;DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}&quot;, Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(&quot;CMSAzureAccountName&quot;).ToString(), Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(&quot;CMSAzureSharedKey&quot;).ToString()));
  </code></pre>
  <p>to</p>
  <pre><code>CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(&quot;DiagnosticsConnectionString&quot;));
  </code></pre>
  <p>But I will mark &quot;Gaurav Mantri&quot; response as correct. If it wasn't for his insight, my image would not have uploaded to the blob.</p>