title: ASP.NET (MVC) Serving images
body: <p>I am creating a MVC 3 application (although just as applicable to other technologies e.g. ASP.NET Forms) and was just wondering if it is feasible (performance wise) to serve images from code rather than using the direct virtual path (like usual).</p>
  
  <p>The idea is that I improve the common method of serving files to:</p>
  
  <ol>
  <li>Apply security checks</li>
  <li>Standardised method of serving files based on route values</li>
  <li>Returning modified images (if requested) e.g. different dimentions (ok this would only be used sparingly so don't relate this to the performance question above).</li>
  <li>Perform business logic before allowing access to the resource</li>
  </ol>
  
  <p>I know HOW to do it but I don't know IF I should do it.</p>
  
  <ol>
  <li>What are the performance issues (if any)</li>
  <li>Does something weird happen e.g. images only load sequentially (maybe that's how HTML does it currently i am not sure - exposing my ignorance here).</li>
  <li>Anything else you can think of.</li>
  </ol>
  
  <p>Hope this all makes sense!</p>
  
  <p>Thanks,
  Dan.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>OK - lets get specific:</p>
  
  <p>What are the performance implications for using this type of method for serving all images in MVC 3 using a memory stream?  Note: the image url would be GenericFetchImage/image1 (and just for simplicity - all my images are jpegs). </p>
  
  <pre><code>public FileStreamResult GenericFetchImage(string RouteValueRefToImage)
  {
      // Create a new memory stream object
      MemoryStream ms = new MemoryStream();
  
      // Go get image from file location
      ms = GetImageAndPutIntoMemoryStream(RouteValueRefToImage);
  
      // return the output as a file
      return new FileStreamResult(ms, "image/jpeg");
   }
  </code></pre>
  
  <p>I know that this method works, because I am using it to dynamically generate an image based on a session value for a captcha image.  It's pretty neat - but I would like to use this method for all image retrieval.</p>
  
  <p>I guess I am wondering in the above example if this is ok to do or whether it requires more processing to perform and if so, how much?  For example, if the number of visitors were to multiply by 1000 for example, would the server be then processingly burdened in the delivery of images..</p>
  
  <p>THANKS!</p>