Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

I did not create this, but thought I would add a useful trick found -->here<--.

Just add this to your control and use it appropriately.

public static bool IsDesignMode
{
  get 
  { 
    return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain"); 
  }
}

Or for us VB.NET folks:

  Public Shared ReadOnly Property IsDesignMode() As Boolean
    Get
      Return AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain")
    End Get
  End Property
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top