Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • sun 403 posts 395 karma points
    Nov 11, 2012 @ 07:21

    How about add WebImage helper example code to this package?

    mvc3 include a new helper WebImage, it's useful, I don't know how to use it in umbraco, can you add one to show how to use it?

  • Chris Koiak 700 posts 2626 karma points
    Nov 11, 2012 @ 14:05
    Chris Koiak

    Hi Sun,

    Thanks for the feedback. This package is based on showcasing the possibilities of Umbraco and to help developers get started, therefore I don't think it's the best place for an example on WebImage.

    I'm sure if you post your problem in the more general forums you'll get the help you require.



  • Damian Green 452 posts 1433 karma points
    Dec 03, 2012 @ 14:28
    Damian Green

    Hi Sun,

    I had a go at creating this to get my head around the Surface Controllers and got it working. It would make a handy little package actually if it was beefed up with error handling etc..

    First you need an ImageResult class that inherits from ActionResult:

    public class ImageResult : ActionResult
         public ImageResult(Stream imageStream, string contentType)
           if (imageStream == null)
               throw new ArgumentNullException("imageStream");
            if (contentType == null)
                throw new ArgumentNullException("contentType");
             this.ImageStream = imageStream;
           this.ContentType = contentType;
         public Stream ImageStream { get; private set; }
         public string ContentType { get; private set; }
        public override void ExecuteResult(ControllerContext context)
             if (context == null)
                throw new ArgumentNullException("context");
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = this.ContentType;
             byte[] buffer = new byte[4096];
            while (true)
                 int read = this.ImageStream.Read(buffer, 0, buffer.Length);
                if (read == 0)
              response.OutputStream.Write(buffer, 0, read);

    This handles the output stream.

    Next create an ImageHandlerSurfaceController that is going to service the requests for the images:

    public class ImageHandlerSurfaceController : Umbraco.Web.Mvc.SurfaceController
        public ImageHandlerSurfaceController()
        public ActionResult GetImage(string filename, int width, int height)
            string path = Server.MapPath(filename);
            if (!System.IO.File.Exists(path))
                return new ImageResult(new MemoryStream(), "binary/octet-stream");
            WebImage img = new WebImage(path);
            img.Resize(width, height);
            return new ImageResult(new MemoryStream(img.GetBytes()), "binary/octet-stream");

    Now all you have to do is call this method from your Views with an img and pass in your params. 

     <img align="right" src="@Url.Action(actionName: "GetImage", controllerName: "ImageHandlerSurface", routeValues:
                filename = "~/media/713/chris_avatar.png",
                width = 300,
                height = 300

    Obviously you would want to get the image from the media object etc but im sure that will get you going.

    The Image handler could be extended to add all the watermark stuff etc also but you have the structure to get you going.


Please Sign in or register to post replies

Write your reply to: