title: How to convert JPEG to WebP format using .NET Core?
body: <p>I have an ASP.NET Core 3.1 WebAPI that is responsible for providing other apps I have with images. I am trying to improve the performance of my apps. One of the feedback I get from running <a href="https://tools.pingdom.com" rel="nofollow noreferrer">PageSpeed Insights</a> test is to <a href="https://web.dev/uses-webp-images/?utm_source=lighthouse&amp;utm_medium=unknown" rel="nofollow noreferrer">Serve images in next-gen formats</a>. </p>
  
  <p>All of the images that my WebAPI server are in JPEG format. I don't know much about image compression or image-formats, but I would like to evaluate the results from converting JPEG images to WebP as <a href="https://developers.google.com/speed/webp/docs/c_study" rel="nofollow noreferrer">study</a> show that WebP leads to better results. </p>
  
  <p>I use the following <code>ImageProcessor</code> class to save uploaded images to my WebAPI.</p>
  
  <pre><code>public class ImageProcessor
  {
      public Image GetResizedImage(Stream stream, int newWidth, out ImageFormat imageFormat)
      {
          Image sourceImage = Image.FromStream(stream);
          SizeF thumbSize = GetNewSize(sourceImage, newWidth);
  
          imageFormat = sourceImage.RawFormat;
          return ResizeImage(sourceImage, (int)thumbSize.Width, (int)thumbSize.Height);
      }
  
      protected SizeF GetNewSize(Image img, int newMaxWidth)
      {
          var size = new SizeF
          {
              Width = newMaxWidth,
              Height = (newMaxWidth * img.Height) / img.Width
          };
  
          return size;
      }
  
      protected Bitmap ResizeImage(Image image, int width, int height)
      {
          var destRect = new Rectangle(0, 0, width, height);
          var destImage = new Bitmap(width, height);
  
          var xRes = Math.Max(Math.Min(image.HorizontalResolution, 96), 72);
          var yRes = Math.Max(Math.Min(image.VerticalResolution, 96), 72);
  
          destImage.SetResolution(xRes, yRes);
  
          using (Graphics graphics = Graphics.FromImage(destImage))
          {
              graphics.CompositingMode = CompositingMode.SourceCopy;
              graphics.CompositingQuality = CompositingQuality.HighQuality;
              graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
              graphics.SmoothingMode = SmoothingMode.HighQuality;
              graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  
              using (ImageAttributes wrapMode = new ImageAttributes())
              {
                  wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                  graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
              }
          }
  
          return destImage;
      }
  }
  </code></pre>
  
  <p>Here is how I call the <code>ImageProcessor</code> class to store images on disk from the controller</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Store(IFormFile file)
  {
      if(!ModelState.IsValid) 
      {
          return Problem("Invalid model!");
      }
  
      // Process Image
      Image image = GetResizedImage(file.OpenReadStream(), 1024, out ImageFormat imageFormat);
      MemoryStream memoryStream = new MemoryStream();
      image.Save(memoryStream, imageFormat);
      memoryStream.Seek(0, SeekOrigin.Begin);
  
      // Store it on disk
      string fullPath = "full path to the new image";
      Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
  
      using FileStream cacheWriter = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, 4096, true);
      await memoryStream.CopyToAsync(cacheWriter);
  
      return Ok();
  }
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>How can I store the image in a WebP format using .Net Core?</p>