Click here to Skip to main content
15,885,954 members
Articles / Programming Languages / C#

An Easy Technique to Improve the Drawing of Background Images of Windows Forms

Rate me:
Please Sign up or sign in to vote.
1.83/5 (5 votes)
18 Jul 2008CDDL1 min read 34.5K   497   17   1
Improve drawing of controls with transparent background over a form with image background

Introduction

This article is about a simple trick to improve the drawing performance of Windows Forms. When a background image is used for a form and it has a number of controls, and especially when there are a lot of controls with a transparent background, you will definitely face an issue in rendering controls. You can individually render the controls just after loading the form. Even if you enable AllPaintingInWmPaint and do double-buffering using SetStyle, the performance will not improve.

(Double-buffering can create problems in a limited memory environment/ a terminal server environment / if you want to use OpenGL applications like VTK to draw over your window.)

This article shows you how to overcome this drawing issue without even using double-buffering.

Using the Code

The technique is overriding the background image property to return an image (Bitmap) which GDI can easily draw. Also, you can include the code to adjust the size. This will avoid the zoom/stretching needed while drawing and further improve performance.

MC++
//C++ Source code
property System::Drawing::Image^ BackgroundImage 
{
    virtual System::Drawing::Image^ BackgroundImage::get()override
    {
        return this->m_bmpRenderBitMap;
    }

    virtual void BackgroundImage::set(System::Drawing::Image ^value) override
    {
        if(nullptr==value)
        {
            m_bmpRenderBitMap = nullptr;
            return;
        }
        //Create new BitMap Object of the size 
        this->m_bmpRenderBitMap = gcnew System::Drawing::Bitmap(value->Width, 
           value->Height, 
           System::Drawing::Imaging::PixelFormat::Format32bppPArgb);

        //Create graphics from image
        System::Drawing::Graphics ^g = 
          System::Drawing::Graphics::FromImage(this->m_bmpRenderBitMap);

        //set the graphics interpolation mode to high
        g->InterpolationMode = Drawing::Drawing2D::InterpolationMode::High;
        
        //draw the image to the graphics to create the new image 
        //which will be used in the onpaint background
        g->DrawImage(value, Rectangle(0, 0, this->m_bmpRenderBitMap->Width, 
                        this->m_bmpRenderBitMap->Height));
    }
}

The same can be done in C# as well:

C#
//C# Sample
public override Image BackgroundImage
{
    get
    {
        return bmpBackground;
    }
    set
    {
        if (value!=null)
        {
            //Create new BitMap Object of the size 
            bmpBackground = new Bitmap(value.Width,value.Height);

            //Create graphics from image
            System.Drawing.Graphics g = 
               System.Drawing.Graphics.FromImage(bmpBackground);
    
            //set the graphics interpolation mode to high
            g.InterpolationMode = 
               System.Drawing.Drawing2D.InterpolationMode.High;
    
            //draw the image to the graphics to create the new image 
            //which will be used in the onpaint background
            g.DrawImage(value, new Rectangle(0, 0, bmpBackground.Width, 
                        bmpBackground.Height)); 
        }
        else
            bmpBackground = null;
    }
}

After posting this article, when I searched on Google, a similar article was found in MSDN blog: http://blogs.msdn.com/mhendersblog/archive/2005/10/12/480156.aspx.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionMy vote of 4 Pin
Jason Newland8-May-19 2:53
Jason Newland8-May-19 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.