EMGU VSIX Project Templates (V2.3)
Example Project Used Within Article
Example VSIX Project Template
The following tutorial explains how to generate your own custom Microsoft project templates. A custom template can save large amounts of time if your projects are complex and based upon custom dynamic link libraries and many external components. While the following tutorial is based upon using C# and EMGU all the processors remain true and can be ported over to generating your own projects.
This tutorial is designed so that user can export the projects among a community or work colleagues with ease. Custom project templates can be made in 2008 however exporting them requires installation in specific folders. This can change between users and VS 2010 solves this issue by introducing VSIX project templates.
Pre-Requisites & Assumed Knowledge
Unfortunately to generate a custom template you will need Visual Studio 2010, with Service Pack 1 and the SP1 SDK. Service Pack 1 is available here, VS 2010 SP1. The SP1 SDK is available here, VS 2010 SP1 SDK
Ensure you restart visual studio after installation so that you can use the extension properly.
It is assumed that a reader is comfortable within the C# environment, can create their own project. Understands how to import files and what dynamic link libraries are used for. While some of this will be covered it won’t contain enough detail for a complete novice.
So what is a Project Template
A project template is what you see when you start a new project in visual studio. It is an empty project in effect, that contains all the pre-requisites you require to start creating an application. Take a standard windows form how does VS know to put a new form with code behind in your project, link that as the start up form and even call it Form1. Well it uses a template, this is the case over many different applications Word, Excel and even your phone will have templates for items such as multimedia messages and texts.
So why should you use a project template?
Templates save us time, save time and we save money. Does this mean you should create one for everything you create? No... A project template is a tool and as such should only be used when necessary. EMGU is used within this example for the reason that using a project template is ideal. It is a complex project that requires many different external components, including custom DLLs.
So why should you use a project template?
Let’s imagine you develop a custom DLL for your company say for advertisements and your company has a logo with a default about box and set icon, a fair assumption. Now imagine that your company adopts you DLL and every individual who develops any program for your company now uses it, along with your company logo, about box and icon. This means that every program they create the first thing they must do is reference you DLL and then import the logo into their project. Then import the about box existing form into their project. A complex system already and prone to mistakes.
Now with a custom project template , when a user creates a new project they use the custom template and all this is done for them. While I’m sure you’d like imagine your bosses will thank you so much offer you a promotion and a big pay rise let’s face it this won’t happen but at least you will have benefited mankind and maybe prevented some of your colleagues from throwing themselves/or computers out the window.
The First Step – Do the Dirty Work
Now to create the project template you must first create the project. There is one vital step for creating your custom projects and this is in the use of custom DLLs. So Don’t just skim through this section.
Adding DLL references
Now project templates will not include externally referenced DLL. It will include any file you include within the project however. So create a folder in project explorer and add your DLLs to this folder as existing items. In this example they will be place in a folder called Lib, the required DLL’s are:
Your solution explorer will now look something like this:
The folder is not needed but it keeps things neat. Now reference the copied DLLs within your projects as you would normally, using browse ...project folder.../DLL/mydllfile.dll
You will be able to check later to make sure you followed this step correctly
Adding Additional Files
Add images and additional files or forms as you would within a normal project. You can use directory structures as we did with the DLL files to keep things neat. In the EMGU example the OpenCV wrapped libraries are require directly within the bin directory. Don’t forget to set properties such as Copy Always... as these will be copied to your project template. In the example only two addition files are need to support basic image processing techniques.
You must first layout your form the way you wish in this case a simple picture box is to be placed on the form in which when the program starts and image is to be drawn and displayed inside it. Not very adventurous but you can have as many controls and in the case of EMGU the designs of which a user may develop is endless.
The Code Behind
The code behind is also added to the project template so if you a method that is always used but an additional class is not needed then included it similarly if you’ve added classes why not put the code behind to invoke them. In our case we are only going to use a simple Hello World type of code. It creates a new image and writes EMGU in the centre, this is then displayed. All this does is test that the application is working and the rest is up to the user.
Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 400, new Bgr(0, 0, 0)); MCvFont f = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 1.2, 1.2); img.Draw("EMGUcv", ref f, new System.Drawing.Point(135, 180), new Bgr(0, 255, 0)); pictureBox1.Image = img.ToBitmap();
Now you have your project run it and test it, fix any bugs as you want your project template to be running as soon as it is opened.
Creating the Project Template
Creating a project template is made incredibly simple, just left click File=>Export Template. If you have not saved your changes you will be prompted to do so before continuing. We want to do things properly so we are not exporting our project as a vsix just yet.
The Export Template Wizard will now appear we want to export the whole Project Template so just select Next >
You can now set up the template details such as:
- Template Name
- Template Description
- Icon Image
- Preview Image
You can leave these blank however you may find you want to add your own else some default ones are used that are less desirable. It also makes thing mor desirable. In this example we will use an EMGU icon and a screen shot that was prepared earlier. If need be you can select cancel at this point gather your items and repeat the last few steps.
Once you have selected your item ensure you deselect the “Automatcally import the template into Visual Studio” checkbox. We will test that we can use the project later but if you forget skip to “Installing and Uninstalling on a Client Machine” section to remove the project.
The template will be saved in a default location as a zip file, you can see that there are several template that were made for the EMGU template project release. The default directory will open automatically in windows explorer but should you close it they are located here: \Documents\Visual Studio 2010\My Exported Templates
You can explorer the project folder using an appropriate tool to ensure all your files have transferred correctly. Repeat this process for multiple templates as required until you have all your required template.
Creating the VSIX export template
Now we have our project template we are going to create a VSIX project so in VS select New Project. Look for the Sub-Contents of Extensibility, you should see a VSIX project option as shown bellow.
If you do not see this project type make sure you have installed Microsoft Visual Studio Service 1 SDK, links are under the “Pre-Requisites” heading.
When you VSIX project opens you will be instantly presented with the source extension manifest file. Fill in the basic Fields such as Product Name, Author, Version and Description. The ID will have a long string of random characters leave these as they are a unique identifier but you can edit the text pre-ceding them.
You now need to select the editions your project template will work with this is vital, the default will be the version you are using. You may want to limit the use to just Professional version of VS or allow multiple languages to use the template.
We are now at the stage of adding the content to out VSIX project, our project template. Left Click the “Add Content” button and a pop up will appear to guide you through. Select the contents type as a “Project Template”
Then select File and navigate to your custom project template .zip file we generated in the last section.
Now if we have several project templates we will want to create the all under a sub-directory for a New project creation. To do this enter the name you wish to use in the Add to subfolder field. If you wish to add the custom project template to the root directort leave it blank. If you want to do both then you will need to add two identical project files one with no sub-folder set and with the sub-folder you desire. In this example a Custom Project sub-folder will be used.
You will now see your project file within then Solution Explorer window. You can always reorganise the structure of you VSIX project here.
Continue adding you project templates as required until you have the structure you would like. Once complete it is time to build the solution so select menu item Build=>Build Solution or press F6
Navigate to the Bin directory if your project, you will now see within it a .vsix project file. If you want to view inside the file rename the extension to .zip you can arrange directories here as well but this is were errors can occur. The VSIX file is all you need so copy and paste this to the desktop and now an installation test can be carried out
Installing and Un-Installing on a Client Machine
To install the VSIX project on a machine ensure that VS 2010 is installed and exited. You can install the VSIX template with VS still open however it won’t appear until you close and restart VS. You will be presented with an Installation wizrard asking which version of VS to install the templates projects to, select the appropriate ones and click Install. If you have generated all the file correctly a success message will be shown
Now re-open VS and select new project navigate to your chosen sub-directory if you used one or look down the list of available templates and you should see your nested happily within them.
Now if you have just done a test project and you want to remove the template it is extremely simple. Select menu option Tool=>Extension Manager.
The Extension Manager will appear showing all your extension such as you custom template projects. Simply select the extension and select the Uninstall Button. A prompt will appear to make sure you wish to uninstall it simply click Yes. No restart of VS is required.
 12/03/2012 Beta Release of EMGU project Templates
 27/032012 Official Release and Article Published