views:

181

answers:

2

I have code:

using (Graphics g = control.CreateGraphics())
{
    Bitmap bm = new Bitmap(r.Width, r.Height, g);
    using (Graphics gbm = Graphics.FromImage(bm))
    {
        gbm.FillRectangle(Brushes.Green, r);
        form.BackgroundImage = bm;
        form.BackgroundImageLayout = ImageLayout.Zoom;
    }
}

But that FillRectangle doesn't seem to have any effect. Any idea?

A: 

Have you tried making FillRectangle the last graphics related thing done in this block? The subsequent Background stuff is likely causing OnPaint to fire again resulting in your rectangle being painted over. Depending on what method this code snippet appears in, this will probably happen every time a repaint occurs.

Dinah
A: 

Perhaps you could try copying your code into a new blank project to see if FillRectangle still has no effect. Maybe something elsewhere in your code is the culprit.

James Lawruk
Yes, like an idiot that I am, I forgot the Rectangle r is located outside the bitmap.
Jiho Han