欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# GDI winfrom 图像转换椭圆形

程序员文章站 2024-03-18 19:59:22
...

现在好多移动端上的人物头像都是椭圆形的,就行winfrom 上能不能设置椭圆形的图像

C# GDI winfrom 图像转换椭圆形

后台代码

 

        /// <summary>
        /// 选择图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnxuztx_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "图片|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif";
            var dialog = open.ShowDialog();
            if (dialog == DialogResult.OK)
            {
               imageName = System.IO.Path.GetFileName(open.FileName);
               fname = open.FileName;
            }
           //获取原始图像
            pictureBox1.Image = outimage(fname);
          
        }
        /// <summary>
        /// 根据文件路径转出image图像
        /// </summary>
        /// <param name="fname"></param>
        /// <returns></returns>
        public Image outimage(string fname)
        {
            using (var fileStream = new System.IO.FileStream(fname, System.IO.FileMode.Open))
            {
                byte[] bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);

                var memoryStream = new System.IO.MemoryStream(bytes);
                if (memoryStream != null)
                {
                    return Image.FromStream(memoryStream);
                }
                return null;
            }
        }
 /// <summary>
        /// 重绘原始图像,变椭圆
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public Bitmap yximage(Bitmap img)
        {
            int t = 0;
            //图片可能会很大,宽高取小
            if (img.Width > img.Height)
                t = img.Height;
            else
                t = img.Width;

            int w = t, h = t;
            Size size = new Size(w, h);
            //先初始化一个位图对象,来存储截取后的图像
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            Rectangle rec = new Rectangle(0, 0, w, h);
            
            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(Point.Empty, size));
                }
            }
            return bitmap;
 
        }
      /// <summary>
        /// 转换图像圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonZH_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                Bitmap bitiamge = (pictureBox1.Image as Bitmap);
                Image ima = yximage(bitiamge);           
                pictureBox2.Image = ima;
            }         
        }
     /// <summary>
        /// 保存图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnbc_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter="图片|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image.Save(sf.FileName);
            }
        }