c# winfrom 在panel上绘制矩形

Bitmap b = new Bitmap(panel1.Width, panel1.Height);
            Graphics g = Graphics.FromImage(b);
            Rectangle rect = new Rectangle((panel1.Width / 2) - 128, (panel1.Height / 2) - 152, 256, 304);
            g.DrawRectangle(new Pen(Color.Lime, 2), rect);
            g.Dispose();
            panel1.BackgroundImage = b;

也可以这么用

Graphics g = panel1.CreateGraphics();