title: xamarin forms get byte array from image control
body: <p>In my application I let the users select an image (of any size).  The application will then load it to an image control, resize it if needed, and display it on the screen.  </p>
  
  <p>I have all the saving / loading from the preferences working correctly right now as I use the Xamarin Forms Plugin.FilePicker and grab the byte array from that to save to my preferences.  </p>
  
  <p>The challenge I am facing is that if the uses pics a large image from their device, the large version of the image is what is uploaded to the FilePicker plugin and the byte array is too large to save.  (I get the error ' The size of the state manager setting value has exceeded the limit.')</p>
  
  <p>So what I would like to do is take the contents of the image control, which have been resized to a manageable size, convert that to a byte array and then save that in my preferences.</p>
  
  <p>Any idea how I can convert the content of the image control into a byte array so I can serialize it in JSON and save it to my preferences??  Below is that code that saves the Byte Array from the file picker.  </p>
  
  <pre class="lang-cs prettyprint-override"><code>private async void btnChooseFile_Clicked(object sender, System.EventArgs e)
  {
      try
      {
          FileData fileData = await CrossFilePicker.Current.PickFile();
          if (fileData == null)
              return; // user canceled file picking
  
          //lblFilePath.Text = fileData.FileName;
  
          imgIcon.Source = ImageSource.FromStream(() =&gt; fileData.GetStream());
  
          // THIS IS THE LINE OF CODE I NEED TO CHANGE TO IT SAVES THE 
          // BYTE ARRAY OF THE SMALLER IMAGE AS DISPLAYED BY THE 
          // IMAGE CONTROL INSTEAD OF THE FULL SIZE FILE THE USER
          // SELECTED 
          ViewModelObjects.AppSettings.KioskIcon = fileData.DataArray;
  
      }
      catch (Exception ex)
      {
          System.Console.WriteLine("Exception choosing file: " + ex.ToString());
      }
  
  }
  </code></pre>