title: Image cropping losing quality MVC3 .Net C# Application
body: <p>I'm having trouble cropping an image in an MVC3 C# application. I am cropping the image but its losing quality when rendered back in a view.
  The image to be cropped is loaded from a database and is created from a ByteArray like so...</p>
  
  <pre><code>public static Image ByteArrayToImage(byte[] byteArrayIn)
  {
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
  }
  </code></pre>
  
  <p>When this image is rendered on a view it looks fine and is off the expected quality.
  I am then selecting a region to crop and using the below method to do the crop... </p>
  
  <pre><code>public Image CropImage(System.Drawing.Image Image, int Height, int Width, int StartAtX, int StartAtY)
          {
              Image outimage;
              MemoryStream mm = null;
              try
              {
                  //check the image height against our desired image height
                  if (Image.Height &lt; Height)
                  {
                      Height = Image.Height;
                  }
  
                  if (Image.Width &lt; Width)
                  {
                      Width = Image.Width;
                  }
  
                  //create a bitmap window for cropping
                  Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                  bmPhoto.SetResolution(Image.VerticalResolution,Image.HorizontalResolution);
  
                  //create a new graphics object from our image and set properties
                  Graphics grPhoto = Graphics.FromImage(bmPhoto);
                  grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                  grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                  grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
  
                  //now do the crop
                  grPhoto.DrawImage(Image, new Rectangle(0, 0, Width, Height), StartAtX, StartAtY, Width, Height, GraphicsUnit.Pixel);
  
                  // Save out to memory and get an image from it to send back out the method.
                  mm = new MemoryStream();
                  bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                  Image.Dispose();
                  bmPhoto.Dispose();
                  grPhoto.Dispose();
                  outimage = Image.FromStream(mm);
  
                  return outimage;
              }
              catch (Exception ex)
              {
                  throw new Exception("Error cropping image, the error was: " + ex.Message);
              }
          }
  </code></pre>
  
  <p>This cropped image is then converted back to a ByteArray  and can be saved into a database like so...</p>
  
  <pre><code>public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
  {
      MemoryStream ms = new MemoryStream();
      imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
      return ms.ToArray();
  }
  </code></pre>
  
  <p>When this image is later rendered on the view its quality is a lot worse than the original image. It looks quite pixilated.
  The original image in this case is .jpg but could be any format.</p>
  
  <p>This is an image of the image after it has been loaded from the database and being cropped...</p>
  
  <p><img src="https://i.stack.imgur.com/cC7C1.png" alt="Precropped image"></p>
  
  <p>This image is the result of the crop. As you can see its not good.</p>
  
  <p><img src="https://i.stack.imgur.com/UvHmr.png" alt="enter image description here"></p>
  
  <p>I have seen some other posts on the topic but they haven’t helped. Can anyone suggest a solution?</p>
  
  <p>Thank you</p>