Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the border color of a picturebox (winform)?

I want to set the border color/style around the picturebox on and off according to different events.

Are there properties or functions that help me to achieve that aim?

like image 924
NewOrder Avatar asked Mar 13 '11 16:03

NewOrder


People also ask

How do you change border color in PictureBox?

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.

What is use of PictureBox in C#?

Typically the PictureBox is used to display graphics from a bitmap, metafile, icon, JPEG, GIF, or PNG file. Set the Image property to the Image you want to display, either at design time or at run time.

How do you move images in PictureBox?

To move the image, click the move button, click on the image, keep hold the clicked mouse button and drag.


1 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!

like image 132
Jim Simson Avatar answered Sep 21 '22 09:09

Jim Simson