简体   繁体   English

如何在 PictureBox 控件上创建彩色边框?

[英]How Do I Create a Colored Border on a PictureBox Control?

I have an PictureBox and an Image in PictureBox1.Image property.我在PictureBox1.Image属性中有一个 PictureBox 和一个 Image。
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:要更改边框颜色,请从 Picturebox 控件的 Paint 事件处理程序中调用它:

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.要动态更改边框颜色,例如从鼠标单击事件中,我使用图片框的 Tag 属性来存储颜色并调整图片框的 Click 事件以从那里检索它。 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:然后,图片框 Click 事件将是这样的:

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;你需要using System.Drawing; at the beginning and don't forget to call pictureBox1.Refresh() at the end.在开始时不要忘记在最后调用pictureBox1.Refresh() Enjoy!享受!

You can't set the size and color of the border of a PictureBox .您无法设置PictureBox边框的大小和颜色。
But you can do a little trick to accomplish that.但是你可以做一个小技巧来做到这一点。

Set your image to the BackgroundImage property.将您的图像设置为BackgroundImage属性。
Set the BackgroundImageLayout to Center .BackgroundImageLayout设置为Center
Change the BackColor property to the color you want the border to be.BackColor属性更改为您希望边框的颜色。
Now resize the PictureBox enough to show the back color, which will now visually act like a border.现在调整PictureBox大小以显示背景颜色,现在它在视觉上就像一个边框。

You can also use the Padding property to accomplish the last step.您还可以使用Padding属性来完成最后一步。

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.您可以通过继承System.Windows.Forms.PictureBox并覆盖PictureBox类的OnPaint方法来创建自己的 PictureBox,从这里使用System.Windows.Forms.ControlPaint类使用“DrawBorder”方法绘制自定义边框并传入来自“OnPaint”方法的“System.Windows.Forms.PaintEventArgs”。

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 .在图片picturebox后面放置一个label
  2. Change the back color of the label to the color of the wanted border.label的背景颜色更改为所需边框的颜色。
  3. Set label 's AutoSize property to false and Resize the label as you want.labelAutoSize属性设置为false并根据需要调整label大小。

Sample:样品:

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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