12

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

1
  • Do you understand what it is you're asking to do?
    – jcolebrand
    Commented Dec 15, 2010 at 4:35

5 Answers 5

12

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!

1
10

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.

2
  • This method has some draw backs tried to use it without success in a complex layout, found overriding OnPaint() method of the PictureBox worked best.
    – user692942
    Commented Apr 20, 2015 at 16:13
  • 1
    This also has trouble when using transparent images. Commented Oct 5, 2017 at 18:20
3

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.

1
  • 2
    If it's a one off you can always just call ControlPaint.DrawBorder from your PictureBox controls Paint event handler.
    – user692942
    Commented Nov 13, 2012 at 14:02
0

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:

enter image description here

1
  • 1
    even we can set panel and other controls as well instead just a label Commented Jul 21, 2022 at 10:30
0

To achieve this goals, I used a button with background imagem and set FlatApparence property

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.