C# 给pictureBox绘制边框

实心矩形

Graphics g = pictureBoxRet1.CreateGraphics();
Rectangle rect = new Rectangle(pictureBoxRet1.ClientRectangle.X, pictureBoxRet1.ClientRectangle.Y,
                                 pictureBoxRet1.ClientRectangle.X + pictureBoxRet1.ClientRectangle.Width + 5,
                                 pictureBoxRet1.ClientRectangle.Y + pictureBoxRet1.ClientRectangle.Height + 5);
SolidBrush b = new SolidBrush(Color.Red);
g.DrawRectangle(b,rect);
------------------------------------------------------------------

透明矩形

Graphics g = pictureBoxRet1.CreateGraphics();
Pen pen = new Pen(Color.Red, 2);
//g.Clear(this.BackColor);
g.DrawRectangle(pen, pictureBoxRet1.ClientRectangle.X, pictureBoxRet1.ClientRectangle.Y,
                           pictureBoxRet1.ClientRectangle.X+pictureBoxRet1.ClientRectangle.Width,
                           pictureBoxRet1.ClientRectangle.Y+pictureBoxRet1.ClientRectangle.Height);

------------------------------------------------------------------------

清除矩形

pictureBoxRet1.Invalidate();

你可能感兴趣的:(C# 给pictureBox绘制边框)