在pictureBox1绘画事件中,我尝试在pictureBox1中的图像周围绘制一个矩形:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(0, 0, pictureBox1.Image.Width,
pictureBox1.Image.Height));
}
但是我得到的是:
而且我还尝试自己绘制一个围绕pictureBox1的矩形:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Green, 0, 0
, pictureBox1.Width, pictureBox1.Height);
}
但是在这种情况下,我只会在左侧和顶部的右侧和底部获得一条粗绿线,而没有绿色。
在设计程序中,将其属性SizeMode中的pictureBox1设置为StretchImage,在两种情况下如何绘制矩形?
以及我调用的第一行的属性如何?这不是身高高低吗?如果我只想在pictureBox的顶部查找和绘制,怎么称呼它?
在画框内绘制很容易:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
float penWidth = 5F;
Pen myPen = new Pen (Brushes.Red, (int)penWidth);
e.Graphics.DrawRectangle(myPen, penWidth / 2F, penWidth / 2F,
(float)pictureBox1.Width - 2F * penWidth,
(float)pictureBox1.Height - 2F * penWidth);
myPen.Dispose();
}
要在图片框外部绘制图片,您需要知道图片框下方的控件。例如,如果它是您的表格,则使用表格paint:
private void Form1_Paint(object sender, PaintEventArgs e)
{
int lineWidth = 5;
Brush myBrush = new SolidBrush (Color.Green);
e.Graphics.FillRectangle(myBrush, pictureBox1.Location.X - lineWidth,
pictureBox1.Location.Y - lineWidth, pictureBox1.Width + 2 * lineWidth,
pictureBox1.Height + 2 * lineWidth);
myBrush.Dispose();
}
我正在使用FillRectangle,因为在图片框下的部分不可见,并且更容易控制宽度。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句