• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C#圆形头像框制作并从数据库读取

c# 搞代码 4年前 (2022-01-09) 29次浏览 已收录 0个评论

现在只要是有关头像的框基本都是圆形的了,C#提供的PictureBox控键默认情况下是方形的非常大的影响美观

PictureBox默认情况下

窗体加载时:

 pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\1.png");

为了方便演示,在窗口加载的时候加载出图片

图片加载出来后然后,把框框变圆的

 GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(pictureBox1.ClientRectangle);
Region region = new Region(gp);
  pictureBox1.Region = region;//赋值
 gp.Dispose();//释放资源
  region.Dispose();//释放资源

只有简单的几行

效果图:

一下美观了很多

接下来演示从数据库读取图片到框框上:

同样也是,写在窗口的加载事件

GraphicsPath gp = new GraphicsPath();

            gp.AddEllipse(pictureBox1.ClientRectangle);

            Region region = new Region(gp);

            pictureBox1.Region = region;

            gp.Dispose();

            region.Dispose();



            CheckForIllegalCrossThreadCalls = false;
            username.Text = 登录界面.jmuser;
            Thread th = new Thread(a);
            th.IsBackground = true;
            th.Start();
            us = username.Text;

            首页 form = new 首页();
            form.TopLevel = false;
            form.Parent = panel1;
            panel1.Controls.Add(form);//将子窗体载入panel            
            form.Show();

            SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();

            bu.Password = "0";
            bu.UserID =<i style="color:transparent">本文来源gaodai$ma#com搞$代*码6网</i> "0";
            bu.DataSource = "0-0";
            bu.InitialCatalog = "0";
            SqlConnection b = new SqlConnection(bu.ToString());
            string str = "select * from  user1  where  username ='" + 登录成功界面.us + "'";
            b.Open();

            SqlCommand c = new SqlCommand(str, b);

            SqlDataReader d = c.ExecuteReader();

            while (d.Read())
            {
                //  label2.Text = d["userid"].ToString();
                string s = d["头像"].ToString();
                byte[] imageBytes = Convert.FromBase64String(s);
                MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
                memoryStream.Write(imageBytes, 0, imageBytes.Length);
                Image image = Image.FromStream(memoryStream);
                this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                this.pictureBox1.Image = image;
            }
      }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#圆形头像框制作并从数据库读取
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址