Tuesday, July 19, 2011

Using WebImage in WinForms applications

System.Web.Helpers namespace contains useful class WebImage which is able to load an image from a stream or a file, transform it and render in browser:



Alternatively you could save the image to a file. But if you try using WebImage class in WinForms application this way you'll get an error. A short investigation with .NET Reflector shows what you have to set up static property System.Web.HttpContext.Current because WebImage.Save() method uses it to resolve file path. It checks the property for null in the very beginning and raises an exception.

Setting up looks like:



We don't need real HTTP context, so this is enough to make things work.