Search code examples
c#imagebackgroundcontrast

c# Override high-contrast for background image


I wasn't able to find an answer for this:

I have a background image on my form that I want to remain visible even when the system is on High Contrast mode. Is there code that can be entered that overrides the HC mode?

I've tried this on the Form Load event but no luck- no definition for graphics. (Not sure this would even be a viable solution):

OnPaint: e.Graphics.DrawImage(new Bitmap(BackgroundImage), 0, 0);

Aside from creating a PictureBox across my form and putting the image that way, does anyone know of a way to show the BG image of the form always?


Solution

  • Override the OnPaintBackground method:

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.DrawImage(new Bitmap(BackgroundImage), e.ClipRectangle);
    }
    

    This DrawImage overload will stretch the image to fit the rectangle. If the ClipRectangle doesn't work (sorry, I can't test this right now!), create a new Rectangle with the background dimensions