TAGS :Viewed: 7 - Published at: a few seconds ago

[ FillRectangle has no effect on Graphics from Bitmap? ]

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?

Answer 1


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.

Answer 2


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.