C# Get the specified "QQ avatar" and draw a "circular avatar frame" GDI(Graphics)

Effect picture:

insert image description here
Complete code (detailed explanation below)

 private void textBox1_TextChanged(object sender, EventArgs e)
        {
    
    //这里是文本框的事件 值发生 改变时发生

            StringBuilder UserID = new StringBuilder(20);
            //值经常发生改变 使用StringBuilder 并且开辟20字符的空间

            Regex r = new Regex(@"[\d]");
            //正则表达式 过滤字符  \d表示只要0-9的数字
            //也就是全部数字

            MatchCollection match = r.Matches(textBox1.Text);
            //需要匹配的字符串 

            foreach (Match user in match)
            {
    
    //使用循环 过滤不需要的字符串

                UserID.Append(user);
                //过滤好的字符串 添加进StringBuilder
            }

            if (UserID.Length <= 4)
            {
    
    
                //判断匹配好的字符串是否大于4
                //因为QQ最低是5位数...
                return;
            }


            Thread th = new Thread(() => beg(UserID.ToString()));
            //创建线程      把 StringBuilder 值传递过去

            th.IsBackground = true;
            //设置成后台线程

            th.Start();
            //开始线程
        }

Thread execution method:

 public void beg(string id)
        {
    
    
            //线程执行的方法体


           try
            {
    
    
                HttpWebRequest beg = (HttpWebRequest)WebRequest.Create("http://q1.qlogo.cn/g?b=qq&nk="+id+"&s=2");
                //发送请求 

                beg.Timeout = 5000;
                //请求的时间为5秒 超过就停止请求

                HttpWebResponse wb = (HttpWebResponse)beg.GetResponse();
                //接收服务器返回的请求

                Stream s = wb.GetResponseStream();
                //把回来的请求变 一个 流

                using (Image i = new Bitmap(s))
                {
    
        //把流传递过来

                    Bitmap b = new Bitmap(50, 50); //初始像素值

                    using (Graphics g = Graphics.FromImage(b))
                    {
    
    //使用Gdi画图  在图片上画 

                        g.SmoothingMode = SmoothingMode.HighQuality;
                        //图片的抗锯齿

                        using (GraphicsPath p = new GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate))
                        {
    
    
                            p.AddEllipse(0, 0, i.Width, i.Height);//添加椭圆

                            g.FillPath(new TextureBrush(i), p);//填充里面

                            pictureBox1.Image = b;
                            //赋值给图片框
                        }

                    }

                }
            }
            catch
            {
    
    

            }

        }

Foreword:
The comment area module of a forum, I found that this function is very good, I pondered that I made roughly the same one night, and used it as the avatar binding function of the registration module.

A forum comment area module (animation)
insert image description here

Suggestion on the function of registration module:
When users register, they can upload their avatars selectively.

The interface to get QQ avatar:

http://q1.qlogo.cn/g?b=qq&nk=This is the QQ number &s=2 Here is the size

http://q1.qlogo.cn/g?b=qq&nk=972001531&s=2

size identifier Size
1 40 × 40
2 40 × 40
3 100 × 100
4 140 × 140
5 640 × 640
40 40 × 40
100 100 × 100

Choose according to the size of your avatar frame

Even the URl of an avatar
Please add image description
can also be downloaded from the local upload server, etc... Research by yourself

With the interface, the next step is to use C# to implement the function...


Interface diagram:
for the convenience of demonstration, no other functions are done... simply build a form
insert image description here
insert image description here

It can be seen from the figure that each time a number is modified, the avatar will be re-acquired,
so the code is written in the event of the text box.
Please add image description
The event means that it occurs whenever the content of the text box changes.

Filter input characters:
Please add image description

Considering that some messy strings may be entered, for
example: 9720ada15
, it cannot be recognized as a QQ number, so it needs to be filtered out and only numbers are recognized

Regular Expression Filtering Characters:
Defining Rules

 Regex r = new Regex(@"[\d]");
//正则表达式 过滤字符  \d表示只要0-9的数字
   或者
 Regex r = new Regex(@"[0-9]");         

Store the matched string

  StringBuilder UserID = new StringBuilder(20);
  //存储过来好的字符串 并且开辟20字符的空间

match string:

 foreach (Match user in match)
            {
    
    //使用循环 过滤不需要的字符串

                UserID.Append(user);
                //过滤好的字符串 添加进StringBuilder
            }

Determine the matching length

 if (UserID.Length <= 4)
            {
    
    
                //判断匹配好的字符串是否大于4
                //因为QQ最低是5位数...
                return;
                //不大于4就返回
            }

Use the interface QQ interface:
because the interface is a web page, you need to send a request, you
need to use

HttpWebRequest: Send a web page request
HttpWebResponse: Receive a request sent by the server
insert image description here
But there is another problem when sending a request, that is, if the network speed is slow or the server response speed is slow, it will cause the software to freeze, but this problem can be solved through threads...

Define the thread:

Thread th = new Thread(() => beg(UserID.ToString()));
    //创建线程      把 StringBuilder 值传递过去
    
    th.IsBackground = true;
    //设置成后台线程

    th.Start();
    //开始线程
}

Thread execution method:

Make a request:

 HttpWebRequest beg = (HttpWebRequest)WebRequest.Create("http://q1.qlogo.cn/g?b=qq&nk="+id+"&s=2");
                //发送请求 
 beg.Timeout = 5000;
                //请求的时间为5秒 超过就停止请求

Receive return request:

   HttpWebResponse wb = (HttpWebResponse)beg.GetResponse();
        //接收服务器返回的请求 流的方式

Use stream conversion:
After getting the stream, you can save the image to the local, etc...

  Stream s = wb.GetResponseStream();
                //把回来的请求变 一个 流

The picture is turned into a circle:
The picture box is a square by default,
insert image description here
and there is no direct attribute to become a circle...
Here use GDI to continue drawing

using (Image i = new Bitmap(s))
                {
    
        //把流传递过来

                    Bitmap b = new Bitmap(50, 50); //初始像素值

                    using (Graphics g = Graphics.FromImage(b))
                    {
    
    //使用Gdi画图  在图片上画 

                        g.SmoothingMode = SmoothingMode.HighQuality;
                        //图片的抗锯齿

                        using (GraphicsPath p = new GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate))
                        {
    
    
                            p.AddEllipse(0, 0, i.Width, i.Height);//添加椭圆

                            g.FillPath(new TextureBrush(i), p);//填充里面

                            pictureBox1.Image = b;
                            //赋值给图片框
                        }

                    }

                }

insert image description here
This is done…

Pure hand play, give a thumbs up~

Guess you like

Origin blog.csdn.net/dpc5201314/article/details/121297233