title: Convert image To Bytes in Asp.Net Mvc 6
body: <p>In Asp.Net Mvc 6, I am trying to convert image to bytes for adding the picture into database. I cannot find the way to use the right method of encoding. Can someone help me to correct this below code, the mistake is at encoding: UInt32 which is not valid in the given context.</p>
  
  <pre><code>private readonly ApplicationDbContext _context = new ApplicationDbContext();
  
      public int UploadImageInDataBase(IFormFile file, PublisherInfos publisherInfos)
      {
          publisherInfos.CoverImage = ConvertToBytes(file);
          var pubInfos = new PublisherInfos
          {
              ImageSize = publisherInfos.ImageSize,
              FileName = publisherInfos.FileName,
              CoverImage = publisherInfos.CoverImage
          };
          _context.PublisherInfos.Add(pubInfos);
          int i = _context.SaveChanges();
          if (i == 1)
          {
              return 1;
          }
          else
          {
              return 0;
          }
  
      }
  </code></pre>
  
  <p>// ConvertToBytes</p>
  
  <pre><code>private byte[] ConvertToBytes(IFormFile image)
  {
      byte[] CoverImageBytes = null;
      var _reader = new StreamReader(image.OpenReadStream());
      BinaryReader reader = new BinaryReader(_reader.ReadToEndAsync, encoding: UInt32);
      CoverImageBytes = reader.ReadBytes((int)image.Length);
      return CoverImageBytes;
  }
  </code></pre>
  
  <p>// Controller</p>
  
  <pre><code> public IActionResult Create(PublisherInfos publisherInfos)
      {
          if (ModelState.IsValid)
          {
              IFormFile file = Request.Form.Files["CoverImage"];
              PublisherInfosRepository service = new PublisherInfosRepository();
              int i = service.UploadImageInDataBase(file, publisherInfos);
              if (i == 1)
              {
                  // Add file size and file name into Database
                  _context.PublisherInfos.Add(publisherInfos);
                  _context.SaveChanges();
                  return RedirectToAction("Index", new { Message = PublisherInfoMessageId.DataloadSuccess });
              }
          }           
  
          return View(publisherInfos);
      }
  </code></pre>