Sobel Edge detection " destabilizes" the runtime

Jan 12, 2012 at 12:14 AM


I have been trying your edge detection methods on a simple .bmp image . The image is a 32 bpp bitmap.   I  properly referenced to Utilities.Media.Image.ExtensionMethods in order to use SobelEdgeDetection() on my 24 bpp bitmap. It compiles fine ( under .NET 4.0 ) but when debugging the app, Visual studio stops at the call of SobelEdgeDetection(), triggers a VerificationException and saying " This operation could destabilize the runtime". I tried to set the security level to 1 using [assembly: SecurityRules(SecurityRuleSet.Level1 )] in assemblyInfo.cs, but it doesn't change anything ( level "none" isn't accepted ). Here is  the code :

 unsafe private void Start_Click(object sender, EventArgs e)
        Bitmap bm = new Bitmap(@"C:\Users\Floent\Documents\Arduino\ASCOMCamera\1StarC1.bmp", true);         

            Viewer.BackgroundImage = bm;

            BitmapData bmd = bm.LockBits(new Rectangle(0, 0, bm.Width , bm.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bm.PixelFormat);
            Bitmap bmfilter = bm.SobelEdgeDetection(); // Debug stops here.



I also tried  with an image converted to a 24 bpp bitmap using the lockbits method. But still,  no change. The Laplace version of the detector fails too with the same error.

Could you help me on that please ?

Jan 12, 2012 at 12:35 AM

Hum sorry this not the correct code. You can remove the line with the lockbits method. I just forgot to remove this before posting. Of course, the error is still there.


Jan 12, 2012 at 3:26 PM

First, make sure that you're allowing unsafe code in the app whenever you use any of the bitmap extension methods (go into the properties of the project and it should be an option under Build that says "Allow unsafe code"). Also you don't need to lock the image. Actually locking the image may cause an issue but not the one you're experiencing as each of the extension methods uses "unsafe code" in order to get a speed increase.

Jan 12, 2012 at 5:52 PM

Unsafe code is allowed. I was already playing with unsafe code before trying to your functions. This is why there is a call to lockbits in the snippets above. It's just a ghost from a previous version of the app that I forgot to remove. When trying with a 24 bpp bitmap , I was locking the image in order to convert  from 32 to 24 bpp and unlocking it before calling the sobel edge detector. But nevermind, consider that I just load the 32 bpp bitmap, and then call your  function  with unsafe code properly allowed. This is enough to trigger the exception mentioned above.

Jan 12, 2012 at 7:40 PM

OK, I've figured it out (I think, or at least I was able to get the same error message). I believe you're using the individual DLL for Utilities.Media. It appears that there is indeed a bug as it should be compiled with [assembly: SecurityRules(SecurityRuleSet.Level1)] in the AssemblyInfo.cs file. It wasn't (I tend to use the full DLL in my apps so I missed this one). I'm going to push to the source code a fix tonight. If you're using NuGet, I push a nightly build to it around 8PM eastern time (assuming it passes the unit tests). Otherwise you'll need to either use the full library (which doesn't have the issue) or pull the code from source and compile it.

Jan 12, 2012 at 9:58 PM

You were right. I was indeed working with the individual dll. Now with the full, it's working properly. Thank you :)