views:

357

answers:

1

I'm executing a line of code that fails to execute within the designer, causing all of my control's public properties to no longer show up in the designer. Because of this I can no longer see any of my forms that use that control in the Visual Studios Design View.

The line of code in question calls an unsafe code project that does a bit of image processing; commenting it out makes the design view come back to life. However, the code executes perfectly, so I cannot see why the code fails in the designer. This is the code that is being called:

        /// <summary>
        /// Performs a color adjustment on the source bitmap. 
        /// </summary>
        /// <param name="source">The bitmap to be processed. This is changed by the
        /// process action.</param>
        /// <param name="redChange">change to the red value. Can be negative.</param>
        /// <param name="greenChange">change to the green value. Can be negative.</param>
        /// <param name="blueChange">change to the blue value. Can be negative.</param>
        /// <returns></returns>
        public static Bitmap ProcessColor(Bitmap source, int redChange, int greenChange, int blueChange)
        {
            sourceBitmap = source;

            // lock the source bitmap
            sourceBitmapData = getBitmapData(sourceBitmap, ref sourceWidth);
            sourcepBase = (Byte*)sourceBitmapData.Scan0.ToPointer();

            PixelData* pPixel;

            for (int y = 0; y < source.Height; y++)
            {
                pPixel = (PixelData*)(sourcepBase + y * sourceWidth);
                for (int x = 0; x < source.Width; x++)
                {
                    int redVal = pPixel->red + redChange;
                    if ( redVal <0 ) redVal = 0;
                    if ( redVal > 255) redVal = 255;
                    pPixel->red = (byte)redVal;

                    int greenVal = pPixel->green + greenChange;
                    if ( greenVal <0 ) greenVal = 0;
                    if ( greenVal > 255) greenVal = 255;
                    pPixel->green = (byte)greenVal;

                    int blueVal = pPixel->blue + blueChange;
                    if (blueVal < 0) blueVal = 0;
                    if (blueVal > 255) blueVal = 255;
                    pPixel->blue = (byte)blueVal;

                    pPixel++;
                }
            }

            sourceBitmap.UnlockBits(sourceBitmapData);
            sourceBitmapData = null;
            sourcepBase = null;

            return source;
        }

(Courtesy of the OpenNETCF Community)

My project is not marked as unsafe, but the project that the above code is in is marked unsafe. Do both projects need to be marked unsafe?

Or, is there a way I can block that line of code firing in the designer (I don't actually need the output of this code whilst in design view as it's just generating a disabled version of an image from a provided image).

EDIT: Blocking the code from running does not fix the issue. Only commenting the line out allows the design view to work. Having the line in (even when put in an if[false == true] statement) causes the designer to display errors, instead of the form.

+5  A: 

Wrap the part of code in if:

if(!DesignMode)
{
    // Your "unsafe" code goes here
}

If you're using Compact Framework, use this:

if(Site != null && !Site.DesignMode)
{
    // Your "unsafe" code goes here
}

See this post on mode info on what a beast DesignMode actually is.

Anton Gogolev
There's no 'DesignMode' in the CF as far as I can see. Or is it in another reference?
GenericTypeTea
Yep, thanks. Missed that. Edited my answer.
Anton Gogolev
I found: if (!this.Site.DesignMode) { // etc }But that doesn't stop the line of code executing
GenericTypeTea
The DesignMode property doesn't return the correct value if you use it in the constructor.
Frederik Gheysels
It's not used in the constructor, it's used in the Set{} of a property.
GenericTypeTea
I'm accepting this as a correct answer for the title of this question, but it does not fix my problem. I'll start a new question.
GenericTypeTea