Click here to Skip to main content
13,865,187 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,I'm trying to use emgucv in C#.when using the following code,I got the exception "The type initializer for 'Emgu.CV.CvInvoke' threw an exception."please help I'm new in this field,If any one gives me some more information,I would be so thankful.
using Emgu.CV.Structure;
using Emgu.CV;

IntPtr ima = Emgu.CV.CvInvoke.cvCreateImage(s, Emgu.CV.CvEnum.IPL_DEPTH.IPL_DEPTH_16S, 10);
Updated 29-Sep-11 9:34am
C_Johnson 20-Sep-11 12:52pm
I have written an article to help you and others ins setting up an EMGU project. It is available here

I hope it helps
ready to learn 22-Sep-11 10:28am
Thanks a lot Chris.Your article helped me a lot,as I found your answer the best one,I want to ask you another guestion:"what does each class in emgucv do and how can I use them?"Is there an e-book which can help?Thanks again in advance
C_Johnson 29-Sep-11 14:21pm
Hi Sorry I was doing a little housework re-tagging questions and I've only just found your comment. Unfortunately I can't tell you what classes do exactly what it doesn't really matter either, if you use the basic two opencv_core220.dll and opencv_imgproc220.dll you can do most simple tasks start a project with them and keep programming until you get an error asking for cvextern.dll at this point you need to add almost all of them as it looks for all of them to work. As for books on EMGU well there are none :s however there are two for opencv if you get these from a library or on-line then they can act as a guide you will hoever have to convert the code. This is easier than it sounds all cv functions are available by calling cvInvoke.FunctionName(...) hope this helps, the links for the books are: [1],or.r_gc.r_pw.&biw=1280&bih=736&um=1&ie=UTF-8&tbm=shop&cid=7984438732523221659&sa=X&ei=JrWETvn4Bo7t-gbK7ZAa&ved=0CFQQ8wIwAQ [2],or.r_gc.r_pw.&biw=1280&bih=736&um=1&ie=UTF-8&tbm=shop&cid=15231969889310013850&sa=X&ei=JrWETvn4Bo7t-gbK7ZAa&ved=0CFAQ8wIwAA
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

EMGU [^]

On that page, you'll find a link to the api documentation AND a tutorial.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


This is a very common problem it is down to the correct dll's not being present in the output folder. EMGU is a wrapper for opencv which means you need the EMGU dll for the c# code (that has been written to deal with the images) and the dll from opencv. Now as Opencv is in fact c++ they can't be referenced as normally in c#.

To see what files are missing when the error pops up at the bottom are Actions: if you select View Details and expand System.TypeInitializedException using the small arrow to the left. The InnerException will show you which dll.


Now the easy way is to go into "bin folder of the EMGU extraction folder and copy all of the .dll files except for those starting with EMGU and paste them into your bin\Debug or bin\Release folder of your c# project.

Now this make your release harder however makes the project less clutered in Visual Studio


More complicated but preferred start with opencv_core220.dll and opencv_imgproc220.dll. These are the two minimal items you will require to load and display an image.

Right click on your project in solution explorer and select Add >> Existing Item... or menu item Project>Add Existing Item.

Find the two files and add them to your project.

Now select them within the solution explorer and go to there properties. You need to change the Copy to Output Directory option to Copy Always.

This will copy the dll every time you compile your project be it a release or debug compilation.


The two suggested dll's will not do a majority of the more complex algorithms. You will get an error of Unable to load cvextern.dll in this case you will have to add a majority of the other opencv dll's. You will need all but:


Unless you are using CUDA then you will need the first two ZedGraph is used to plot graphs in c# and usually copied automatically anyway.

There are additional instructions here if you get stuck:[^]

Hope this Help,

Take Care
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I meet the same question. I find that the program from here runs very well, but when I write the same program and download opencv_XXXX242.dll files from the latest version, it can not work. Then I delete the latest one, and add the program's file,opencv_XXXX231.dll. It run very well.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

you need some DLLs;
to find which DLLs you need, copy all dlls from "...emgucv\bin\x86" to "...\bin\Debug" folder.
run your program.
try to delete all dll while your program is open!!
windows delete all dlls that dont used!
all remaining dlls are necessary.

for me this DLLs was needed:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190214.1 | Last Updated 6 Dec 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100