Posts

Identifying the most dominant color in an image

Hi, I developed an algorithm that identifies the most dominant color in an image. The input is a URL to the target image and the output is the dominant color. The algorithm reads all the pixels of the bitmap and maps them into a dictionary. Then, it builds a graph of similar colors by a pre-defined threshold. While building the graph, the algorithm maintains the most dominant color by the node that has the largest amount of pixels combined with its' adjacent nodes. public class ColorsGraph { private const int _similarityThreshhold = 30; private readonly Dictionary<Color, int> _colorsMapping; private Dictionary<ColorNode, List<ColorNode>> _colorsGraph; private KeyValuePair<ColorNode, List<ColorNode>> _mostDominantColor; public ColorsGraph(string imageUrl) { _colorsMapping = new Dictionary<Color, int>(); var request = WebRequest.Create(imageUrl); using (var response = request.GetResponse...

Uploading files asynchronously in asp.net mvc site

Hi, I've been looking for quite some time for a complete solution to uploading files in ajax for my site. For those of you who don't know, browsers won't let you upload files without making a full page reload. I haven't been able to find a good, working and cross browser solution for this problem. So after much research and development I have finally managed to solve this problem. Lets take a look at the following controller. public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult UploadImage(HttpPostedFileBase image) { try { if (Request.ContentLength > 1024 * 1024 * 2) { return UploadCompanyLogoResult(new { success = false, error = "File size too big. Maximum file size is 2MB." }); } if (Request.Files.Count == 0) { return UploadCompanyLog...

Coupling properties with friendly names

Hey, For some time I've been looking for a good way to couple between the property, and it's display name. Some may say it's a bad practice, but I think sometimes it has it's advantages. So I solved it using DescriptionAttribute. The class is in System.dll, so we don't need to add any reference. Let's take a look on the following class: public class UserChecklistEntity { public bool TakeTheTour { get; set; } public int VisitsCount { get; set; } } Let's decorate the properties with DescriptionAttributes: public class UserChecklistEntity { [Description("Take the tour")] public bool TakeTheTour { get; set; } [Description("Visits count")] public int VisitsCount { get; set; } } And now for the interesting part, add this extension method: public static class ObjectExtensions { public static string GetDescription<T>(this T obj, Expression<Func<T, object>> expression) where T : class { ...

Extending cshtml with functions and properties

Hey, Lately I've been building many infrastructures for my company's website and I wanted to expose those infrastructures in the most natural way that coders are used to. One of the problems I spent time on was exposing the infrastructures on *.cshtml files. I didn't want to use static classes - because it's not a natural way, and not very trivial. So I managed to extend the class that represents the view, and by that add more properties and functions to it. public abstract class ViewBase<TModel> : WebViewPage<TModel> where TModel : class { public LayoutConfiguration LayoutConfiguration { get; private set; } public ViewBase() { LayoutConfiguration = LayoutConfiguration.GetInstance(); } public void DoWork() { } } And now on your Web.config (in the Views folder) replace this line: <pages pageBaseType="System.Web.Mvc.WebViewPage"> With this one: <pages pageBaseType="namespace.ViewBase...

Impersonating CurrentUser from SYSTEM

Hey all, I've been searching for a very long time an easy way to impersonate current user while running in a "SYSTEM" user credentials. I haven't found any code that does it and actually works. So I managed to combine pieces of code from several places, and added some of my own. This helper class exposes 2 functions: 1. Get current user impersonated scope - in this scope you can do pretty much whatever you want in the current user credentials. 2. Start a process as current user. public static class ImpersonationUtils { private const int SW_SHOW = 5; private const int TOKEN_QUERY = 0x0008; private const int TOKEN_DUPLICATE = 0x0002; private const int TOKEN_ASSIGN_PRIMARY = 0x0001; private const int STARTF_USESHOWWINDOW = 0x00000001; private const int STARTF_FORCEONFEEDBACK = 0x00000040; private const int CREATE_UNICODE_ENVIRONMENT = 0x00000400; private const int TOKEN_IMPERSONATE = 0x0004; private const int TOKEN_...

Json To Dictionary generic model binder

Hey all, Few days ago I was challenged by a good friend and colleague to write a generic model binder for a dynamic json. Why does he need such a thing ? I'll explain: Normally, when you write a classic website using any platform, the contract between the client and the server is obvious and well known. However, if you write a complex web system, and you have the need to build your client model dynamically without creating a massive model on the server that handles all kinds of properties and values, you might be interested in getting a generic dictionary that contains all your client values. To implement it I used a state machine to parse the json: public class DictionaryModelBinder : DefaultModelBinder { private const string _dateTimeFormat = "dd/MM/yyyy HH:mm:ss"; private enum StateMachine { NewSection, Key, Delimiter, Value, ValueArray } public override object BindModel(ControllerContext controllerC...

Testing controller actions with ActionFilterAttribute in ASP .NET MVC

Hey, Have you ever wanted to test your controller actions with the logic you add with ActionFilterAttributes? Well, I did. So I wrote this helper class, which executes the ActionFilterAttribute methods. public class FilterAttributeTester { public ActionExecutingContext ExecuteOnActionExecuting<TAttribute>(Controller aController, Func<ActionResult> aAction) where TAttribute : ActionFilterAttribute { var actionExecutingContext = CreateActionExecutingContext(aController, aAction); var attribute = GetAttribute<TAttribute>(aAction); attribute.OnActionExecuting(actionExecutingContext); return actionExecutingContext; } public ActionExecutedContext ExecuteOnActionExecuted<TAttribute>(Controller aController, Func<ActionResult> aAction) where TAttribute : ActionFilterAttribute { var actionExecutedContext = CreateActionExecutedContext(aController, aAction); var attribute = GetAttri...