title: Save Layout to bitmap(Xamarin)
body: <p>I have layout and need to save it to bitmap</p>
  
  <p>here is code</p>
  
  <pre><code>public void Save()
  {
      LinearLayout view = FindViewById&lt;LinearLayout&gt;(Resource.Id.badge);
  
      view.DrawingCacheEnabled = true;
      view.BuildDrawingCache();
      Bitmap layout = view.GetDrawingCache(true);
  
  }
  </code></pre>
  
  <p>I set breakpoint to  <code>Bitmap layout = view.GetDrawingCache(true);</code> and I see that's layout is null.</p>
  
  <p>Where is my mistake, how to save layout to bitmap?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I try to save view to bitmap like this</p>
  
  <pre><code>public  Bitmap CreateBitmapFromView(View view, bool autoScale = true)
      {
          var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
          view.DrawingCacheEnabled = true;
          view.BuildDrawingCache(autoScale);
          var bitmap2 = view.GetDrawingCache(autoScale);
          view.DrawingCacheEnabled = wasDrawingCacheEnabled;
          return bitmap2;
  
      }
  </code></pre>
  
  <p>All ok, bitmap2 is returning.</p>
  
  <p>But also I need to save it to SD</p>
  
  <p>I wrote method like this</p>
  
  <pre><code>public  Bitmap CreateBitmapFromView(View view, bool autoScale = true)
      {
          var wasDrawingCacheEnabled = view.DrawingCacheEnabled;
          view.DrawingCacheEnabled = true;
          view.BuildDrawingCache(autoScale);
          var bitmap2 = view.GetDrawingCache(autoScale);
          view.DrawingCacheEnabled = wasDrawingCacheEnabled;
          var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
          var filePath = System.IO.Path.Combine(sdCardPath, "test.png");
          var stream = new FileStream(filePath, FileMode.Create);
          bitmap2.Compress(Bitmap.CompressFormat.Png, 100, stream);
          return bitmap2;
  
      }
  </code></pre>
  
  <p>And have this error.</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.</p>
  </blockquote>