111,092
社区成员




public void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
{
//四边圆角
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
g.DrawPath(p, gp);
gp.Dispose();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawRoundRect(e.Graphics, Pens.Black, 0, 0, pictureBox1.Width - 1, pictureBox1.Height - 1, 10);
}
你上面看的源码是有问题的,我测试过,加载的图片不会呈现。只会呈现一个白色背景圆形
public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
gp.CloseAllFigures();
return gp;
}
// panel的paint
private void panel1_Paint(object sender, PaintEventArgs e)
{
Draw(e.ClipRectangle, e.Graphics, 18,true, Color.FromArgb(90, 143, 0), Color.FromArgb(41, 67, 0));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString("其实我是个Panel", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.White), new PointF(10, 10));
}
public class MyPictureBox : PictureBox
{
protected override void OnCreateControl()
{
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(this.ClientRectangle);
Region region = new Region(gp);
this.Region = region;
gp.Dispose();
region.Dispose();
base.OnCreateControl();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
//Rectangle rec = new Rectangle(0, 0, 72, 72);
var g = pe.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
//g.DrawEllipse(Pens.White, rec);
g.DrawImage(Image, Point.Empty);///
}
}
长宽应在外部调整 wideh 和 height 属性,二不应在内部写死,否则就失去写成控件的意义了