关于图片转为数据流的相关应用(二)

前言

上一篇写了关于头像的一些代码,今天来说一下代码的优化,只要是因为想好多网站也好,客户端也好头像都是圆的,怎样将方方正正的头像变成圆形的呢?就这个问我也是实验了好长时间,在小伙伴的帮助下终于实现了这个功能.

正文

实现的效果如下图:
关于图片转为数据流的相关应用(二)

敲黑板来围观了,主要的代码要改几个地方可以用下边的代码与前一篇(https://blog.csdn.net/qq_39674002/article/details/85344874)中的去对比发现不同的地方

//图片变成圆形的方法
 private Image CutEllipse(Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);


            using (Graphics g = Graphics.FromImage(bitmap))
            {
                if (pictureBox1.Image != null)
                {
                    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(Point.Empty, size));
                    }
                }
            }
            return bitmap;
        }


        public void SaveImage(OpenFileDialog OpenF)//将文件转换成数据流
        {
            string strImage = OpenF.FileName;
            FileStream fs = new FileStream(strImage, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] imageByte = br.ReadBytes((int)fs.Length);
            user.photostr = imageByte;//可以将二进制更新在数据库里(全局变量user)
            if (user.photostr == null)
            {
                lblModify.Text = "请修改头像";
            }
            else
            {
                bool updatePic= facade.UpdateUserPic(user);
            }
            StreamToFile(imageByte);
        }
        
        public void StreamToFile(byte[] imageByte)//将数据流转换成文件
        {
            if (imageByte != null)
            {
                MemoryStream ms = new MemoryStream(imageByte);
                pictureBox1.Image = Image.FromStream(ms);
                int width = pictureBox1.Image.Width;//这是宽
                int height = pictureBox1.Image.Height;//这是高

                //图片变成圆形
                Image image = this.pictureBox1.Image;
                Image newImage = CutEllipse(image, new Rectangle(0, 0, width, height), new Size(150, 150));
                this.lblHead.Image = newImage;
            }
        }
        private void lblHead_Click(object sender, EventArgs e)
        {
                string Photo;
                bool isUpLoadPicture = false;
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    isUpLoadPicture = true;

                    Photo = ofd.FileName;//实际文件的路径
                    pictureBox1.Image = Image.FromFile(Photo);
                    pictureBox1.ImageLocation = Photo;
                    SaveImage(ofd);

                }
        }

结束

代码就是这么的神奇,只有不知道的没有什么是代码做不到的!!!虽然现在知道不是很多,但我相信只要每天进步一点点总会成为大牛的.