【机房合作】——图片

using System.IO;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;//限制输入的引用

图片变圆

#region 图片变圆形的方法
        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;
        }
#endregion
#region 修改图片(保存在数据库,调出数据库图片)

        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);

                }
        }

#endregion

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值