hamed
hamed

Reputation: 163

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?

Upvotes: 12

Views: 28163

Answers (5)

Adelson Silva
Adelson Silva

Reputation: 9

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

Upvotes: 0

Maseed
Maseed

Reputation: 543

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

Upvotes: 0

Jim Simson
Jim Simson

Reputation: 2862

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!

Upvotes: 12

user692942
user692942

Reputation: 16672

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.

Upvotes: 3

BeemerGuy
BeemerGuy

Reputation: 8269

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.

Upvotes: 10

Related Questions