另一个图像周围的圆形边框未在 winforms 中绘制
Circular border around another image is not drawing in winforms
我有一个图片框控件,我把它做成了圆形,现在我想用这个在它周围画一个红色圆圈:
Graphics gf = pictureBoxLastLogin1.CreateGraphics();
gf.DrawEllipse(new Pen(Color.Red, 2), new Rectangle(0, 0, pictureBoxLastLogin1.Width+12, pictureBoxLastLogin1.Height+12));
但是它没有在图像周围绘制任何东西?我做错了什么?请记住,我已使用此代码片段将方形图像转换为圆形图像。
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, pictureBoxLastLogin.Width , pictureBoxLastLogin.Height);
Region rg = new Region(gp);
pictureBoxLastLogin.Region = rg;
一旦您使它无效,windows 就会重新绘制它,这反过来又会删除您在其上所做的任何绘制。
使用Paint
事件,你要在paint事件中画什么都会留在那里
当我们在控件上绘制并且希望绘制持久化时,我们需要订阅Paint() event of that Control. Or, if it's a Custom Control (a custom class derived from an existing object), to override it's OnPaint()方法。
经常重绘控制 DC。当另一个 Window 移到它上面时,当它的 Form
容器被 Minimized/Maximized 或调整大小时,如果它触及控件可见区域等等。
当需要重绘时,引发 Paint()
事件。
只有在 Paint()
事件处理程序(或 OnPaint()
方法)中编码的绘图才会被保留。
同样重要的是要注意大多数对象使用的实现 IDisposable()
.
他们都需要 Disposed()
。此处,GraphicsPath
对象和绘图 Pen
.
应用到 PicturBox
的 Region
也应该被处理掉。它可以在 class 范围内声明并在 Form
关闭时处理。
一个示例,使用(或多或少)与问题中相同的设置。
一个 Form
包含一个 PictureBox
和 2 Buttons
:
单击 Button1
时,将为 PictureBox
.
创建一个椭圆 Region
Button2
将 Invalidate()
PictureBox
,导致重新绘制 预定 。将引发 Paint()
事件。
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
bool PaintBorder = false;
int RegionInSet = 8;
private void button1_Click(object sender, EventArgs e)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(RegionInSet, RegionInSet, pictureBox1.Width - (RegionInSet * 2),
pictureBox1.Height - (RegionInSet * 2));
Region region = new Region(path);
pictureBox1.Region = region;
}
}
private void button2_Click(object sender, EventArgs e)
{
PaintBorder = true;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!PaintBorder) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingMode = CompositingMode.SourceOver;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (Pen penRed = new Pen(Color.Red, 12))
{
int PenRedOffset = (int)(penRed.Width / 2) + (penRed.Width % 2 == 0 ? -1 : 0);
e.Graphics.DrawEllipse(penRed,
new RectangleF(RegionInSet + PenRedOffset, RegionInSet + PenRedOffset,
pictureBox1.Width - (PenRedOffset * 2) - (RegionInSet * 2),
pictureBox1.Height - (PenRedOffset * 2) - (RegionInSet * 2)));
}
}
视觉结果:
我有一个图片框控件,我把它做成了圆形,现在我想用这个在它周围画一个红色圆圈:
Graphics gf = pictureBoxLastLogin1.CreateGraphics();
gf.DrawEllipse(new Pen(Color.Red, 2), new Rectangle(0, 0, pictureBoxLastLogin1.Width+12, pictureBoxLastLogin1.Height+12));
但是它没有在图像周围绘制任何东西?我做错了什么?请记住,我已使用此代码片段将方形图像转换为圆形图像。
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, pictureBoxLastLogin.Width , pictureBoxLastLogin.Height);
Region rg = new Region(gp);
pictureBoxLastLogin.Region = rg;
一旦您使它无效,windows 就会重新绘制它,这反过来又会删除您在其上所做的任何绘制。
使用Paint
事件,你要在paint事件中画什么都会留在那里
当我们在控件上绘制并且希望绘制持久化时,我们需要订阅Paint() event of that Control. Or, if it's a Custom Control (a custom class derived from an existing object), to override it's OnPaint()方法。
经常重绘控制 DC。当另一个 Window 移到它上面时,当它的 Form
容器被 Minimized/Maximized 或调整大小时,如果它触及控件可见区域等等。
当需要重绘时,引发 Paint()
事件。
只有在 Paint()
事件处理程序(或 OnPaint()
方法)中编码的绘图才会被保留。
同样重要的是要注意大多数对象使用的实现 IDisposable()
.
他们都需要 Disposed()
。此处,GraphicsPath
对象和绘图 Pen
.
应用到 PicturBox
的 Region
也应该被处理掉。它可以在 class 范围内声明并在 Form
关闭时处理。
一个示例,使用(或多或少)与问题中相同的设置。
一个
Form
包含一个 PictureBox
和 2 Buttons
:单击
Button1
时,将为 PictureBox
.创建一个椭圆
Region
Button2
将 Invalidate()
PictureBox
,导致重新绘制 预定 。将引发 Paint()
事件。using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
bool PaintBorder = false;
int RegionInSet = 8;
private void button1_Click(object sender, EventArgs e)
{
using (GraphicsPath path = new GraphicsPath())
{
path.AddEllipse(RegionInSet, RegionInSet, pictureBox1.Width - (RegionInSet * 2),
pictureBox1.Height - (RegionInSet * 2));
Region region = new Region(path);
pictureBox1.Region = region;
}
}
private void button2_Click(object sender, EventArgs e)
{
PaintBorder = true;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (!PaintBorder) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingMode = CompositingMode.SourceOver;
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (Pen penRed = new Pen(Color.Red, 12))
{
int PenRedOffset = (int)(penRed.Width / 2) + (penRed.Width % 2 == 0 ? -1 : 0);
e.Graphics.DrawEllipse(penRed,
new RectangleF(RegionInSet + PenRedOffset, RegionInSet + PenRedOffset,
pictureBox1.Width - (PenRedOffset * 2) - (RegionInSet * 2),
pictureBox1.Height - (PenRedOffset * 2) - (RegionInSet * 2)));
}
}
视觉结果: