title: How to get image width with Umbraco Media Service Saving Event
body: <p>I am looking for a way to get the width of an image with an Umbraco event, to validate that the image respects a certain aspect ratio before being uploaded. The thing is I don't know if casting the mediaItem to Image is even possible. How can I get the width or the height here is my event code so far:</p>
  
  <pre><code>public class ImageResizer : ApplicationEventHandler
  {
      protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
          MediaService.Saving += MediaServiceSaving;
      }
      void MediaServiceSaving(IMediaService sender, SaveEventArgs&lt;IMedia&gt; e)
      {
          foreach (var mediaItem in e.SavedEntities)
          {
              Image img = (Image)mediaItem;
              double height = img.Height;
              double width = img.Width;
              string msg = "" + height;
              if (height / width != 1)
              {
                  msg = "Ratio is not 1:1. Please make sure the width and height of your image is the same.";
                  BasePage.Current.ClientTools.ShowSpeechBubble(BasePage.speechBubbleIcon.success, "Error", msg);
              }
              else
              {
                  //Ducplicate image to images and thubnails of desired choice
                  //Send images to database - continue normal process
              }
          }
      }
  }
  </code></pre>