File Conversion Right-click Shell Extension: 25 Projects in 25 Days

As part of the LEAD Technologies 25th anniversary, we are creating 25 projects in 25 days to celebrate LEAD’s depth of features and ease of use. Today’s project comes from Nick.

What it Does

This project shows how to convert any image file format by right-clicking on it using LEADTOOLS Version 19.

Features Used

Development Progress Journal

Hello, I’m Nick and I’ll be creating a shell extension to convert image files.

From a personal experience, I’ve dealt with opening files in high-end graphics programs before for the sole purpose of converting them to another format. Loading up and shutting down these applications can be a time-consuming process just to convert a single file. I’ve also done some batch scripts before to automate conversions from the command line and while this is useful, it’s more so when converting multiple images at once. But, for one at a time, a shell extension which puts these options at a right-mouse click would be ideal.

Using the LEADTOOLS codecs will save quite a bit of time, rather than using .NET code directly. Furthermore, LEADTOOLS makes file conversion for over 150 image file formats quite simple, which will give me more time to focus on the context menu.

I’ve dug up some example code on implementing a context menu. Turns out there are quite a few things I didn’t consider, but I should be able to use this as a starting point for my implementation.

I’ve modified the code to insert registry keys which associates the shell extension with the file formats. It was a bit tedious digging into the registry to learn how the keys were structured, so this took a few hours to complete.

I’ve achieved a proof of concept by registering my extension with the CMP format, so the option “LEADTOOLS Convert” appears as an entry when right-clicking any CMP file in Windows Explorer. However, the menu item has no functionality at this point. It’s taken me maybe four hours to get to this point.

Now I’m wiring in the actual logic to handle the menu item being clicked, and extended the proof of concept to convert the file to a PNG. For this, I’m just loading the file using RasterCodecs.Load(), then saving it back with RasterCodecs.Save() in the new format:
Documentation: RasterCodecs.Load
Documentation: RasterCodecs.Save

I could create a custom GUI that would offer additional format conversions. Luckily, LEADTOOLS has already done the legwork for me by providing RasterSaveDialog. Creating that dialog box, and the background logic that goes with it, took maybe six lines of code, about an hour, and another DLL reference.
Documentation: RasterSaveDialog

At this point, the first time the program runs it integrates the necessary keys into the registry for the file types. After that, right-clicking a file of that type displays “LEADTOOLS Convert” on the context menu. Selecting that option dipslays the LEADTOOLS Save dialog box, where output format, filename, and other options can be set.

A coworker recommended an icon. I extended the program to set registry keys pointing to a default icon file to use. After establishing that the change worked, I tracked down an image of the LEADTOOLS “triangle” logo to use. However, it was a JPG. I right-clicked it, selected LEADTOOLS Convert from the context menu, chose “Ico” as the output format, and hit save, making it easy to add to the project.

Digging up examples, researching, and ultimately ditching one implementation for another took maybe eight hours, but I’m pleased with the results.

Originally, my implementation involved loading the file to be converted into memory as a RasterImage, then to be saved to the output filename and format. However, during peer review, it was suggested I use RasterCodecs.Convert() instead. I modified my program and, using this, was able to perform the image conversion in a single line of code!
Documentation: RasterCodecs.Convert

Download the Project

The source code for this sample project can be downloaded from here. To run the project, extract it to the C:\LEADTOOLS 19\Examples\DotNet\CS directory.


LEAD Technologies, Inc.

This entry was posted in General Imaging and tagged , , , , , , , . Bookmark the permalink.

2 Responses to File Conversion Right-click Shell Extension: 25 Projects in 25 Days

  1. Joe Fox says:

    How do I actually make this project work? I am not a programmer, sorry.

    • Greg says:

      You will need to have Visual Studio installed.

      1) Open project in Visual studio 2008 or above with admin privileges
      2) Run the project

Leave a Reply

Your email address will not be published. Required fields are marked *