简体   繁体   中英

How Do I Create a Colored Border on a PictureBox Control?

I have an PictureBox and an Image in PictureBox1.Image property.
How do I place a border around the Image?

This has always been what I use for that:

To change the border color, call this from the Paint event handler of your Picturebox control:

private void pictureBox1_Paint_1(object sender, PaintEventArgs e)
    {
        ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
    }

To change the border color dynamically, for instance from a mouseclick event, I use the Tag property of the picturebox to store the color and adjust the Click event of the picturebox to retrieve it from there. For example:

if (pictureBox1.Tag == null) { pictureBox1.Tag = Color.Red; } //Sets a default color
  ControlPaint.DrawBorder(e.Graphics, pictureBox1.ClientRectangle, (Color)pictureBox1.Tag, ButtonBorderStyle.Solid);

The picturebox Click event, then, would go something like this:

private void pictureBox1_Click(object sender, EventArgs e)
        {
            if ((Color)pictureBox1.Tag == Color.Red) { pictureBox1.Tag = Color.Blue; }
            else {pictureBox1.Tag = Color.Red; }
            pictureBox1.Refresh();
        }

You'll need using System.Drawing; at the beginning and don't forget to call pictureBox1.Refresh() at the end. Enjoy!

You can't set the size and color of the border of a PictureBox .
But you can do a little trick to accomplish that.

Set your image to the BackgroundImage property.
Set the BackgroundImageLayout to Center .
Change the BackColor property to the color you want the border to be.
Now resize the PictureBox enough to show the back color, which will now visually act like a border.

You can also use the Padding property to accomplish the last step.

Hope that helps.

You can create your own PictureBox by inheriting from System.Windows.Forms.PictureBox and overriding the PictureBox class OnPaint method, from here use the System.Windows.Forms.ControlPaint class to paint your custom border using the 'DrawBorder' method and pass in your 'System.Windows.Forms.PaintEventArgs' from the 'OnPaint' method.

Something like this;

using System.Windows.Forms;
using System.Drawing;

public class CustomPictureBox : PictureBox
{
  protected override void OnPaint(PaintEventArgs e) 
  {
    base.OnPaint(e);
    ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Red, ButtonBorderStyle.Solid);
  }
}

This is just a quick example (untested) to get you started, sorry I can't be more thorough.

I was here because I was facing the same problem. I pointed out a simpler solution and that is.

  1. Place a label behind a picturebox .
  2. Change the back color of the label to the color of the wanted border.
  3. Set label 's AutoSize property to false and Resize the label as you want.

Sample:

在此处输入图片说明

为了实现这个目标,我使用了一个带有背景图像的按钮并设置了 FlatApparence 属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM