title: How to save an image in SQLite in Xamarin Forms?
body: <p>I have the following two methods that handles taking photos from a camera and picking photos from a library. They're both similar methods as at the end of each method, I get an <code>ImageSource</code> back from the <code>Stream</code> and I pass it onto another page which has an <code>ImageSource</code> binding ready to be set. These two method work perfectly. The next step now is to save the Image in SQLite so I can show the images in a ListView later on. My question for the XamGods (Xamarin Pros =), what is the best way to save image in SQLite in 2019? I have been in the forums for hours and I still don't have a tunnel vision on what I want to do. I can either</p>
  
  <ul>
  <li>Convert Stream into an array of bytes to save in Sqlite.</li>
  <li>Convert ImageSource into an array of bytes (messy/buggy).</li>
  <li>Somehow retrieve the actual Image selected/taken and convert that into an array of bytes into SQLite</li>
  </ul>
  
  <p>I'm sorry if my question is general, but Xamarin does not provide a clear-cut solution on how to save images in SQLite and you can only find bits and pieces of solutions throughout the forums listed below.</p>
  
  <ul>
  <li><a href="https://forums.xamarin.com/discussion/135898/how-to-save-and-retrieve-image-from-sqlite" rel="noreferrer">How to save and retrieve Image from Sqlite</a></li>
  <li><a href="https://forums.xamarin.com/discussion/19853/load-image-form-byte-array" rel="noreferrer">Load Image from byte[] array.</a></li>
  <li><a href="https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream">Creating a byte array from a stream</a></li>
  </ul>
  
  <p>Thank you in advance!</p>
  
  <pre><code>    private async Task OnAddPhotoFromCameraSelected()
      {
          Console.WriteLine("OnAddPhotoFromCameraSelected");
  
          var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() { });
          var stream = photo.GetStream();
          photo.Dispose();
          if (stream != null)
          {
              ImageSource cameraPhotoImage = ImageSource.FromStream(() =&gt; stream);
              var parms = new NavigationParameters();
              parms.Add("image", cameraPhotoImage);
              var result = await NavigationService.NavigateAsync("/AddInspectionPhotoPage?", parameters: parms);
  
              if (!result.Success)
              {
                  throw result.Exception;
              }
          }
      }
  
      private async Task OnAddPhotoFromLibrarySelected()
      {
          Console.WriteLine("OnAddPhotoFromLibrarySelected");
          Stream stream = await DependencyService.Get&lt;IPhotoPickerService&gt;().GetImageStreamAsync();
          if (stream != null)
          {
              ImageSource selectedImage = ImageSource.FromStream(() =&gt; stream);
  
              var parms = new NavigationParameters();
              parms.Add("image", selectedImage);
              parms.Add("stream", stream);
              var result = await NavigationService.NavigateAsync("/AddInspectionPhotoPage?", parameters: parms);
  
              if (!result.Success)
              {
                  throw result.Exception;
              }
          }
      }
  </code></pre>