title: Reading an image and storing it into an array in c# xaml
body: <p>I am trying to open an image file (either JPEG or JPG) and store the image into an array in c# xaml. Although I can open the file and convert the file into a bitmap image, I cannot store it into an array. Here is my code:</p>
  
  <pre><code>    public int row;
      public int column;
      public byte[] bmp;
      public byte[,] data;
      public double width;
      public double height;
          private void button1_Click(object sender, RoutedEventArgs e)
      {
          // Create OpenFileDialog
          Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  
          // Set filter for file extension and default file extension
          dlg.FileName = "";
          //dlg.DefaultExt = ".jpg";
          dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
  
          // Display OpenFileDialog by calling ShowDialog method
          Nullable&lt;bool&gt; result = dlg.ShowDialog();
  
          if (result == true)
          {
              string filename = dlg.FileName;
              bmp = readfile(filename);
              int x = bmp.Length;
              data = new byte[(int)Math.Sqrt(x), (int)Math.Sqrt(x)];
              row = 0;
              column = 0;
  
              for (int i = 0; i &lt; x; i++)
              {
                  row = i % (int)Math.Sqrt(x);
                  column = i /(int)Math.Sqrt(x);
                  data[row, column] = bmp[i];
              }
  
  
  
          }
          }
      public byte[] readfile(String filename)
      {
          Image img = new Image();
          BitmapImage bitmapImage = new BitmapImage();
          Uri uri = new Uri(filename);
          bitmapImage.UriSource = uri;
          img.Source = bitmapImage;
  
          MemoryStream memStream = new MemoryStream();              
          JpegBitmapEncoder encoder = new JpegBitmapEncoder();
          encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
          encoder.Save(memStream);
          return memStream.GetBuffer();
          width = bitmapImage.Width;
          height = bitmapImage.Height;
  </code></pre>