Interesting Analysis of the State of Microsoft’s Software Development Tools

The Register published an interesting article on the state of Microsoft’s software development tools today – it focuses on tools for developing desktop apps. Although well written, the article doesn’t make up-beat reading and is in fact quite depressing. WPF and related development tools have been let languish for so long now it is hard to see if it can be brought up to scratch in any reasonable time-frame….

I suppose the message is that things have moved on and that most apps that are consumed on the desktop, except in a few niche areas (machine vision being one of them?), will be delivered on the web via our new friends HTML5, javascript, angular.js et. al.

What do you think? Does Microsoft have the ability or the will to pull out of this nose dive? I hope they do as they have been leaders in this area before and it is a shame to see things decay….

A good / recommended JSON serializer for .NET

One of the great advances in software engineering over the last while is the rise of Json over the more ungainly and awkward XML. It is even making inroads in the more conservative .NET world.

In terms of working with Json in .NET, in my opinion, you can’t really do much better than NewtonSoft’s Json.NET lib, it’s great stuff and makes working with Json .NET very easy (as it should be).

Whatever you do don’t be tempted to use Microsoft’s default Json support as its very awkward and removes much of the advantage of using Json in the first place.

So many thanks to James Newton-King for a great library!

You can add this library to your Visual Studio project via the package manager console bu issuing the following command:

Install-Package Newtonsoft.Json

Happy Json-ing!!

Save 8 bit uncompressed windows bitmap file (.BMP) in c# / .Net

It took me about 3 years to figure this out. In .Net when you save an 8 bit bitmap from software (PixelFormat.Format8bppIndexed) via Save() it is saved by default in a compressed format (as is allowed by the .bmp pseudo-standard).

Now some Machine Vision libraries can’t load compressed 8 bit bitmaps (poor old Cognex, bless) so I had to figure out how to get .Net to save the .bmp file in an uncompressed format.

The following code save the bitmap as 8bit compressed (if the bitmap’s format is PixelFormat.Format8bppIndexed)


Now Microsoft has not documented this whole area very well, but it is quite easy, you have to manually specify an image encoder and set its ‘Compression’ parameters as in the following code:

  // Make sure that the blighter will be saved
  // uncompressed.
  var enc = GetEncoderInfo("image/bmp");
  var parms = new EncoderParameters(1);
  var parm = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
  parms.Param[0] = parm;
  // Save the bitmap.
  bitmap.Save("it.bmp", enc, parms);

Where GetEncoderInfo() is defined as:

private static ImageCodecInfo GetEncoderInfo(String mimeType)
    int j;
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for (j = 0; j < encoders.Length; ++j)
        if (encoders[j].MimeType == mimeType)
            return encoders[j];
    return null;

Here we set the Encoder.Compression parameter to EncoderValue.CompressionNone and this should ensure that the bitmap is saved uncompressed.

See this post for details of how to save an 8 bit greyscale image to disk from a byte array.


Converting between std::string and .NET System::String in C++/CLI

In the software development world it seems that we spend most of our time converting from one data representation to another…..

So with that in mind, Here is a vary handy cheat sheet for how to go about converting to and from standard strings and .NET System::String when using C++/CLI/CLR:

Note that the include paths must be pre-pended by “msclr”, e.g.:


And the symbols live in this namespace: msclr::interop

std::string std_str = msclr::interop::marshal_as(sys_str);

When you #include these headers you may get loads of crazy compile errors, some involve ‘IServiceProvider’, to get rid of these try changing the include order of your headers, you may have to move these marshal includes up closer to the top of your include list.

Windows WPF – Always scroll to end of ListBox when new item added

If anybody is looking for a quick and dirty way of getting a list box to always scroll to its end then have a look here:

Take a look in Item #6, it worked well for me although I am aware that it may be seen somewhat as a hack – but then sometimes WPF just forces you into these situations as it can make it so damn hard to do the simplest of things!

C# Create a Cognex 8bit image (CogImage8Grey) from an 8bit Grayscale image array (byte[])

Here is some code that shows how to create an 8bit grayscale cognex image (CogImage8Grey) from an 8bit raw image stored as a byte array (byte[]). This type of memory messing is difficult in .NET at the best of times and it’s just a pity the the cognex library doesn’t help much more than it does.


I also have a feeling that the cognex library is doing more copying than it strictly needs to, but in true style its software documentation does not detail if, or when, it copies image data (or much else for that matter!) So I copy the image data from the byte array into a malloc’ed buffer before creating the cognex image.


First we have to define a SafeBuffer through which cognex can free up the allocated memory when it is finished with it, to do this we can derive a class from SafeBuffer like this:
[crayon lang=”csharp”]

/// A wrapper around malloc so that FreeHGlobal() is called
/// when the object is disposed.

class SafeMalloc : SafeBuffer

/// Allocates memory and initialises the SaveBuffer

///The number of bytes to allocate public SafeMalloc(int size) : base(true)


/// Called when the object is disposed, ferr the
/// memory via FreeHGlobal().

protected override bool ReleaseHandle()
return true;


/// Cast to IntPtr

public static implicit operator IntPtr(SafeMalloc h)
return h.handle;


Its constructor mallocs the memory and when it is disposed ReleaseHandle() is called, and this frees the memory. I also added a cast to IntPtr so that we can pass it into functions that expect an IntPtr.


Now that we have SafeMalloc we can write function to create the cognex image like this:
[crayon lang=”csharp”]
class CognexStuff
public ICogImage Convert8BitRawImageToCognexImage(
byte[] imageData, int width, int height)
// no padding etc. so size calculation
// is simple.
var rawSize = width * height;

var buf = new SafeMalloc(rawSize);

// Copy from the byte array into the
// previously allocated. memory
Marshal.Copy(imageData, 0, buf, rawSize);

// Create Cognex Root thing.
var cogRoot = new CogImage8Root();

// Initialise the image root, the stride is the
// same as the widthas the input image is byte alligned and
// has no padding etc.
cogRoot.Initialize(width, height, buf, width, buf);

// Create cognex 8 bit image.
var cogImage = new CogImage8Grey();

// And set the image roor

return cogImage;


This function allocates memory via SafeMalloc, it then copies the raw image data from the input array into this memory. Then CogImage8Root.Initialize() is called passing in a pointer to this memory. not that in this case the image’s stride is the same as its width. Once the CogImage8Root has been initialised we can create a CogImage8Grey image and set the root via a call to SetRoot()!


They certainly make you work for it!!


If you know that nobody else will be using your image array and are willing to go ‘unsafe'(!) then you could avoid this extra memory copy by pinning the array and getting a pointer to it, you could then pass this pointer directly to the root Initialize() function. In this case you won’t need the SafeMalloc class etc.


Thanks to all on this thread for hints on SafeBuffer!


.NET – How to tell if a DLL is 32bit or 64bit

So in the world of MS Windows software development, the bitness of DLLs (64bit vs 32bit) is the new DLL Hell!! It is slightly annoying that Microsoft eventually dug itself out of decades of DLL hell just to create a new hellish DLL vista – anyway there’s nothing to be done.


API vendors will often have 2 versions of their API, a 32bit one and a 64bit one, sometimes if you are really unlucky they will name their assemblies and DLLs using the same names for both platforms so there will be no way to easily tell if a DLL is 32bit or 64bit just by looking at the file name.


So how do you figure out the bitness of a DLL when you are trying to debug and fix .NET ‘Incorrect Format’ errors?


Annoyingly it turns out that there is no easy way to do this through windows. There is, however, a nice little tool by Silurian Software that integrates into windows explorer’s file properties pages called InspectEye, it gives you loads of info about an EXE or DLL including whether it’s 32 or 64 bit! It worked nicely for us anyway although I haven’t looked at the rest of the software’s features yet…


The tool can be downloaded from here:


.NET WPF – Provide value on ‘System.Windows. Baml2006.TypeConverter MarkupExtension’ threw an exception.

Here’s a strange WPF error we encountered at a client’s site today:


[code]Provide value on ‘System.Windows.Baml2006.TypeConverterMarkupExtension’ threw an exception.[/code]


If you get this error, then chances are your running your WPF app on Windows XP, and your app will probably work fine on Windows 7. Here is a good post about the problem:


In our case we just had to remove an unneeded Icon attribute from our .xaml Window element and the error went away… I am not quite sure why the error occurred but now it’s gone anyway!


.NET, converting a string to an enum

Here is a handy article that I found which explains how to convert a string into an enum value, it uses the Enum.Parse() function:


Adding a Menu Item to App’s System Menu in .NET

Here’s a good stackoverflow post about how to go about adding a menu item to you app’s system menu in .NET, the system menu is the one that pops down when you click on your app’s icon: