Blog posts


  • How to write code faster in .Net and Flash!

    March 25, 2010 | Tags: Actionscript, ASP.NET, C#, AS3
    Flash/Actionscript
    I've been a fan of the very functional development environment FlashDevelop for programming Actionscript. If you're still using the code editor in Flash for your AS2/AS3 programming needs you should head over and download FlashDevelop immediatly. Much better intellisense and hot keys to do common shores (like for instance pressing Ctrl + Shift + 1, which lets you declare functions, variables, properties etc way much faster than writing all the text yourself).

    Visual Studio/C#/VB.Net
    I've missed the above functionality for a long time, but today I found some great tools to add the same functionality into Visual Studio (and more!). There are some products out there to help speeding up your coding skills, like Resharper (which looks really cool and has a price tag starting at $149) from Jetbrains. The product I've just downloaded to install for free is Devexpress´ lite version of  CodeRush (priced $250) called CodeRush Xpress. If you're on a tight budget you should definitivly download it and give it a try (especially the CTRL + ´ feature!! (which is CTRL + ö on a swedish keyboard). If you got a more generous budget you might find the additional features in the commercial programs listed above of interest.

  • Analyze your .NET assemblies with FxCop

    March 12, 2010 | Tags: ASP.NET, Validation, C#, Troubleshooting
    Microsoft has a nice tool for analyzing your assemblies and recognize hundreds of potential security or performance issues as well as some bad mannered code standards.

    FxCop analyzes programming elements in managed assemblies, known as targets, by using rules that return informational messages about the targets when the rules are violated.

    Download FxCop from Microsoft
    Getting Started with FxCop

  • Javascript library via content delivery networks

    March 08, 2010 | Tags: Javascript, Google
    Are you using any of the following javascript libraries?
    • jQuery
    • jQuery UI
    • Prototype
    • script_aculo_us
    • MooTools
    • Dojo
    • SWFObject
    • Yahoo! User Interface Library (YUI)
    • Ext Core
    • Chrome Frame
    Then you may benefit from using Google's CDN (Content Delivery Network) for AJAX libraries. You'll find it over at Google AJAX Libraries API


    If you're using:
    • jQuery
    • jQuery Validate
    ..you might want to check out Microsoft's CDN at Microsoft Ajax Content Delivery Network

  • Cache functions in C# using memoization

    March 02, 2010 | Tags: ASP.NET, C#, Snippet
    Ever wanted to cache the output from a function depending on the parameters in the call, so - that for instance - a heavy calculation that would always return the same answer for a specific parameter set only was computed once? The answer to this i memoization.

    In computing, memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. Read more about memoization at Wikipedia.

    This is not very suitable for functions that depend on updated data etc. You might want to tweak the routine to handle a timeout for a couple of minutes to get the best out of both worlds, if that's your case.

    Please note that this only works for parameters passed by value. In C# this is the most common data types (string, int, double etc). More complex objects (like arrays or custom objects) is passed by reference.

    Source code (.NET 2.0)

    public delegate TReturn Function<TParam, TReturn>(TParam param);

    static Function<TParam, TReturn> Memoize<TParam, TReturn>(Function<TParam, TReturn> func) {

    Dictionary<TParam, TReturn> cache = new Dictionary<TParam, TReturn>();
    return delegate(TParam arg) {

    TReturn result = default(TReturn);

    if (cache.ContainsKey(arg)) {
    result = cache[arg];
    }
    else {
    result = cache[arg] = func(arg);
    }
    return result;
    };
    }

    Testing the code
    Lets create a dummy function just for testing purposes. It takes a name as inparameter and returns a string
    public string SayHello(string name) {
    return "Hello " + name + "! It's now " + DateTime.Now;
    }

    Setup and call our new function by these lines:
    // Create a memoized version of our function
    var SayHelloFunc = Memoize<string, string>(SayHello);

    string a = SayHelloFunc("Ted");
    string b = SayHelloFunc("John");
    string c = SayHelloFunc("Ted");

    If you set a break point at the first SayHelloFunc call and slowly steps through you'll notice that a and b will contain the exact same timestamp, regardless of the time between the two calls. This is cause their in parameter is the same and the result therefor cached.

    The parameters to the Memoize-function are:
    Memoize(function name);

  • Watermarking the easy way

    March 01, 2010 | Tags: Morgan, Image Library, C#, Snippet
    This post will show how easy it is to do a bit of watermarking using the open source C# Image Library.

    What we'll do
    Watermarks can appear in many different ways. This sample will show how to create the following in just a couple of lines:

    (Image above courtesy of Morgan over at Imaginations. Check out his site for some beautiful CG imagery.)

    Step by step

    Preparations
    First of all, head over to http://code.google.com/p/imagelibrary/ and download the latest DLL (or source code if you want to compile it yourself).

    Secondly, you'll need a watermark to apply to your images. Create a PNG image with very transparent layers in both black and white. The one I've created has black texts in a layer of 10% opacity as well as a white outline also with 10% opacity. (Please note that the watermark will be tiled, in other words used x * y times to cover the image you want to mark.)
    This is my watermark.png (applied on a gray background):


    Finally lets do some coding!
     Create a new project and add the C# Image library  you just downloaded (either the DLL or the project). Enter the following lines (in the appropriate place, depending of what type of project you're developing).
    using Kaliko.ImageLibrary;

    //...

    // Open the image you want to watermark
    KalikoImage image = new KalikoImage("c:\\MyImage.jpg");

    // Apply your watermark PNG image
    image.BlitFill("c:\\Watermark.png");

    // Save new marked image as high-quality jpeg
    image.SaveJPG("c:\\Marked.jpg", 99);

    That wasn't so hard, was it? :) The C# Image library is developed to ease common graphical tasks as this.

    Bookmark and Share