I have an PictureBox and an Image in PictureBox1.Image
property.
How do I place a border around the Image?
5 Answers
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!
-
Glad my original suggestion was helpful (3 years earlier). Commented Jul 21, 2022 at 10:43
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.
-
This method has some draw backs tried to use it without success in a complex layout, found overriding
OnPaint()
method of thePictureBox
worked best. Commented Apr 20, 2015 at 16:13 -
1
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.
-
2If it's a one off you can always just call
ControlPaint.DrawBorder
from your PictureBox controlsPaint
event handler. Commented Nov 13, 2012 at 14:02
I was here because I was facing the same problem. I pointed out a simpler solution and that is.
- Place a
label
behind apicturebox
. - Change the back color of the
label
to the color of the wanted border. - Set
label
'sAutoSize
property tofalse
and Resize thelabel
as you want.
Sample:
-
1even we can set panel and other controls as well instead just a label Commented Jul 21, 2022 at 10:30
To achieve this goals, I used a button with background imagem and set FlatApparence property