A friend recently asked if I could help build a very simple image manipulation console app.
In short, he needed to take a folder full of RAW image files, and generate a smaller thumbnail in various formats (PNG, JPEG etc). Seemed like straight forward and (wrongly), I just assumed that .NET could handle RAW image files natively by now. As it turns out, I was very wrong.
What Image Types Can .NET Handle Natively?
From my research, .NET out of the box (Including Framework, Core and .NET 5+) could handle the following image types :
EMF (Enhanced Metadata File)
EXIF (Exchangeable Image File)
GIF (Needs no explanation!)
ICON (Windows Icon Image Format)
JPEG (Again, no explanation necessary)
WMF (Windows Metadata File)
Notice no “RAW” there. Raw itself actually has a few extensions including .CR2 which is Canon’s proprietary format, along with .NEF, .ORF, .ERF, .SRW and .SR2. The majority of these are just raw formats from a given camera manufacturer. The “Raw” format itself is almost just an overarching term for the images coming directly out of a camera without any processing.
The only sensible option I found out there was to use the Magick.NET wrapper around ImageMagick. ImageMagick itself is a image processing library that is available in many languages (Especially common in PHP), and so it’s a pretty feature rich option.
Other options for .NET mostly included using command line applications, but just triggering them from .NET code using Process.Start. I want to avoid this sort of malarky (Even if at times, that’s what Magick.NET is actually doing behind the scenes).
To install Magick.NET, we first need to install the appropriate nuget package. In reality, there are packages for each CPU type (X86/X64) and different quality types (Q8, Q16, Q16HDRI). I was thinking I would try and go middle of the road and as it happens, that was also the most popular package on Nuget.
So from our Package Manager Console :
From there, our code is actually extremely simple :
using (var image = new MagickImage(“RAW_CANON_EOS_1DX.CR2”))
var geometry = new MagickGeometry();
geometry.IgnoreAspectRatio = false;
geometry.Width = 500;
This generates thumbnails (500px wide) from our images while keeping the same aspect ratio. Pretty nifty!
A quick note on testing all of this too. While building this, I didn’t have a camera myself that could generate raw images, so instead I used https://rawsamples.ch to check that various formats would work through ImageMagick (And they all seemed fine!). But if you aren’t sure which “raw” format you should be using, you can of course ask for the type of camera, and match it up to a couple of samples from the site. Worked a treat for me!