@TOC
背景
使用winform窗体做一个人脸识别的效果,需要使用三个pictureBox来存放gif图,第一个最底下有个圆圈再转,中间显示人脸的图片,第三个显示个来回弹的横线;
提示:以下是本篇文章正文内容,下面案例可供参考
一、将pictureBox变成圆形图片?
pictureBox默认是长方形的,要将其变成圆形需要重写这个控件,只需要在formload事件加载这个方法即可
private void Form1_Load(object sender, EventArgs e)
{
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(picShow.ClientRectangle);//picShow为控件名字
Region region = new Region(gp);
picShow.Region = region;
gp.Dispose();
region.Dispose();
//linePic.Parent = this.picShow;
}
二、实现pictureBox透明
picturebox直接叠加放置,并设置backcolor为透明并不能实现透明; 需要设置需要透明的控件的parent为上一个picturebox;但有个问题就是坐标不是按照直接放置的做来生成了,需要重新设置下location。至于为什么就不知道了,简单记录下,
pictureBox2.Parent = this.picShow;// pictureBox2为那个横线的pictuebox
pictureBox2.Location = new System.Drawing.Point(0, 5);
三、图片裁剪成圆形
如果不需要设置picturebox为圆形看,可以直接裁剪图片为圆形,代码如下:
public Bitmap GetRoundPic(System.Drawing.Image img)
{
int width = img.width;
int height = img.height;
var length = width;
if (width > height)
{
length = height;
}
Rectangle rec = new Rectangle(0, 0, length, length);
System.Drawing.Size size = new System.Drawing.Size(length, length);
Bitmap bitmap = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
{
br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillEllipse(br, new Rectangle(System.Drawing.Point.Empty, size));
}
}
return bitmap;
}